GithubHelp home page GithubHelp logo

human-date's Introduction

human-date NPM Version wercker continuous integration testing

Making dates and times readable for humans.

Quick Start

Install:
npm install human-date --save
Usage:
var hdate = require('human-date')

hdate.prettyPrint("8/16/1987")
// August 16th, 1987

hdate.relativeTime(60 * 60 * 24 * 2 * -1)
// 2 days ago

hdate.relativeTime("8/16/2015")
// 125 days from now

hdate.monthName("8/16/2015")
// August

Methods

.prettyPrint(datestring or jsdate or numseconds, [options])

Arguments:
  • datestring string that can be parsed as a date (Eg. "8/16/1987")
  • jsdate object which is a javascript Date (Eg. new Date("8/16/1987"))
  • numseconds integer seconds to or from the current time (Eg. -32)
  • options: object with the following keys
    • showTime boolean: default: false
Returns:
  • string representing the date. (Eg. "August 16th, 1987")
Examples:
hdate.prettyPrint('8-16-1987')
// August 16th, 1987

hdate.prettyPrint(new Date('8-16-1987'))
// August 16th, 1987

hdate.prettyPrint(-6400)
// November 17th, 2014 (at the time)

hdate.prettyPrint(new Date(1416448704578), { showTime: true })
// November 19th, 2014 at 5:58 pm

.relativeTime(datestring or jsdate or numseconds, [options])

Arguments:
  • datestring string that can be parsed as a date (Eg. "8/16/1987")
  • jsdate object which is a javascript Date (Eg. new Date("8/16/1987"))
  • numseconds integer seconds to or from the current time (Eg. -32)
  • options: object with the following keys
    • futureSuffix string default: "from now"
    • pastSuffix string default: "from now"
    • presentText string default: "now"
    • returnObject boolean: default: false
    • allUnits boolean: default: false
Returns:

Depending on the option returnObject you will get a string or an object:

  • default string "27 years 96 days 21 hours 47 minutes 2 seconds ago"
  • object { seconds: 31, hours: 4, days: 101, years: 27, past: true }
Examples:
hdate.relativeTime(4)
// 4 seconds from now

hdate.relativeTime(4, {futureSuffix: "in the future"})
// 4 seconds in the future

hdate.relativeTime("8-16-1987")
// 27 years ago

hdate.relativeTime(new Date("8-16-1987"))
// 27 years ago

hdate.relativeTime(new Date("8-16-1987"), {returnObject: true})
// { seconds: 31, minutes: 5, hours: 4, days: 101, years: 27, past: true }

hdate.relativeTime(75, {allUnits: true})
// 1 minute, 15 seconds from now

.monthName(datestring or jsdate or monthnum)

Arguments:
  • datestring string that can be parsed as a date (Eg. "8/16/1987")
  • jsdate object which is a javascript Date (Eg. new Date("8/16/1987"))
  • monthnum integer the month number, not 0-indexed (Eg. 12)
Returns:
  • string
Examples:
hdate.monthName(8)
// August

hdate.monthName("8-16-1987")
// August

hdate.monthName(new Date("8-16-1987"))
// August

.toUTC(datestring or jsdate or monthnum)

Arguments:
  • datestring string that can be parsed as a date (Eg. "8/16/1987")
  • jsdate object which is a javascript Date (Eg. new Date("8/16/1987"))
  • epochtime integer time since epoch, in milliseconds (Eg. 1416451096261)
Returns:
  • object native Date object modified to UTC timezone.
Examples:
hdate.toUTC(1000000000000)
// '2001-09-09T08:46:40.000Z'

hdate.toUTC(1000000000000).getFullYear()
// 2001

hdate.toUTC("8-16-1987")
// Sun Aug 16 1987 07:00:00 GMT-0700 (PDT)

hdate.toUTC(new Date("8-16-1987"))
// Sun Aug 16 1987 07:00:00 GMT-0700 (PDT)

hdate.toUTC(new Date("8-16-1987")).toISOString()
// 1987-08-16T14:00:00.000Z

Todos

  • Add various options (prefixes, etc...)
  • Support different locales.

Contributing

Forks and pull requests are most welcomed.

Please add any methods to test.js and run npm test before sending a pull request.

MIT license

Copyright (c) 2014-2016, Montana Flynn (http://anonfunction.com/)

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.