Coder Social home page Coder Social logo

humantime's Introduction

humanTime

humanTime is a super lightweight module aimed at formating time and date in a human readable format, à la Tweeter:

  • express time in minutes if date < 1 hour (3 min, 10 min…),
  • express time in hour if date < 24 hour (1 h, 6 h…),
  • express time with a localized string if more than 24 hour ago (12 décembre, December 12…)
  • express time with a localized string mentionning the year if not the same year as we are (12 décembre 2014, December 12, 2014…)

Under the hood

The module takes advantage of modern browsers' Intl capabilities. It allows for an extremely lightweight module (less than 1kB gzipped) as there is no need to embbed explicit translation and formating rules.

Intl support is quite broad so there's no need to polyfill if you're targetting reasonably modern browsers.

Installation & usage

npm install humantime
// require using commonJS
const humanTime = require('humantime');

// or in es6, using a module bundler like webpack
import humanTime from 'humantime';

// you can pass the function either a plain string
const formattedDateStr = humanTime('2017-11-18T10:11:47.232Z');

// or a Date object
const formattedDateObj = humanTime(new Date());

That's all there is to know!

Contributing

There's sure room for improvement, so feel free to hack around and submit PRs! Please just follow the style of the existing code, which is Airbnb's style with minor modifications.

humantime's People

Contributors

buzut avatar

Watchers

James Cloos avatar Mateusz Burzyński avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.