Making dates and times readable for humans.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
README.md
bower.json
gulpfile.js
humandate.js
humandate.min.js
package.json
test.js

README.md

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/)