Making dates and times readable for humans.
npm install human-date --save
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
- 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
- monthAbbreviated
boolean
: default:false
- showTime
string
representing the date. (Eg."August 16th, 1987"
)
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
hdate.prettyPrint('12-25-1980', { monthAbbreviated: true })
// Dec 25th, 1980
- 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
- futureSuffix
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 }
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
-
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
) -
monthAbbreviated
boolean
whether to abbreviate the month name (Eg.Dec
)
string
hdate.monthName(8)
// August
hdate.monthName("8-16-1987")
// August
hdate.monthName(new Date("8-16-1987"))
// August
hdate.monthName(new Date("8-16-1987"), true)
// Aug
- 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
)
object
native Date object modified to UTC timezone.
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
- Add various options (prefixes, etc...)
- Support different locales.
Forks and pull requests are most welcomed.
Please add any methods to test.js and run npm test
before sending a pull request.
Copyright (c) 2014-2016, Montana Flynn (http://anonfunction.com/)