Making dates and times readable for humans.
Quick Start
Install:
npm install human-date --save
Usage:
var hdate = hdate// August 16th, 1987 hdate// 2 days ago hdate// 125 days from now hdate// 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
- showTime
Returns:
string
representing the date. (Eg."August 16th, 1987"
)
Examples:
hdate// August 16th, 1987 hdate// August 16th, 1987 hdate// November 17th, 2014 (at the time) hdate// 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
- futureSuffix
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// 4 seconds from now hdate// 4 seconds in the future hdate// 27 years ago hdate// 27 years ago hdate// { seconds: 31, minutes: 5, hours: 4, days: 101, years: 27, past: true } hdate// 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// August hdate// August hdate// 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// '2001-09-09T08:46:40.000Z' hdate// 2001 hdate// Sun Aug 16 1987 07:00:00 GMT-0700 (PDT) hdate// Sun Aug 16 1987 07:00:00 GMT-0700 (PDT) hdate// 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/)