nice-date
Tiny dependency-free JavaScript date utility library
Rewrite to clean up & remove mutability coming up.
Installation
npm
Install with npm.
npm i nice-date --save
Require in project.
var NiceDate = ;
bower
bower install nice-date --save
Usage
Instantiate
new NiceDate([date, opts])
Optionally pass a date as the first argument and any default opts you want to override as the second. If you want to only pass options, you can pass null
as the date argument.
var date = null indicators: milliseconds: "milli" ;
daysOf
NiceDate.daysOf(measure)
Extract the number of days within a given time measure, e.g. number of days in the current (relative to instantiated date) week or month. Each day is returned as an array element, represented by a unix timestamp.
var date = ; date; // -> [1425859200000, 1425945600000, ...]date; // -> [1425859200000, 1425945600000, ...] date; // -> [1425164400000, 1425250800000, ...]date; // -> [1425164400000, 1425250800000, ...] date; // -> see belowdate; // -> see below
While the month measure will collect all days between the first and the last day of the month, calendarMonth will in addition collect days between the month start and the first day of that week, as well as the days between the month end and the last day of that week. Useful when working with e.g. a calendar.
date;
Console output:
-> Monday
-> Tuesday
-> Wednesday
-> Thursday
-> Friday
-> Saturday
-> Sunday
get
NiceDate.get(format)
Displays the date in one of a number of formats.
var date = "2015-03-12"; // Get unix timestampdate; // -> 1426118400000date; // -> 1426118400000 // Get day of monthdate; // -> 12date; // -> 12 // Get week day namedate; // -> "Thursday"date; // -> "Thursday" // Get week day short namedate; // -> "Thu"date; // -> "Thu" // Get week number (of year)date; // -> 11date; // -> 11 // Get month namedate; // -> "March"date; // -> "March" // Get month short namedate; // -> "Mar"date; // -> "Mar" // Get yeardate; // -> 2015date; // -> 2015 // Get short yeardate; // -> 15date; // -> 15 // Get date (ISO presentation)date; // -> "2015-03-12"date; // -> "2015-03-12"date; // -> "2015-03-12" // Get datetime (ISO presentation) (timezone CET in example)date; // -> "2015-03-12 01:00"date; // -> "2015-03-12 01:00"date; // -> "2015-03-12 01:00"date; // -> "2015-03-12 01:00" // Get timedate; // -> "01:00"date; // -> "01:00"
diff
NiceDate.diff([format])
Displays the time difference between the date and now.
// now: 2015-03-12 10:00var past = "2015-03-12 09:52" future = "2015-03-12 11:59" waaay = "2018-03-12"; past; // -> "8m ago"future; // -> "in 2h"waaay; // -> "in 3y" // You can also pass a formatting parameter to get a raw valuepast; // -> 8 // Available formatting optionspast; // alias: "mi"past; // alias: "h"past; // alias: "d"past; // alias: "w"past; // alias: "m"past; // alias: "y"
add
NiceDate.add(number, format)
Add number amount of format to a date.
var date = "2015-03-12 08:00"; date; // -> "2015-03-12 08:10"date; // -> "2015-03-12 08:10"date; // -> "2015-03-12 08:10" date; // -> "2015-03-13"date; // -> "2015-03-13"date; // -> "2015-03-13" date; // -> "2015-03-19"date; // -> "2015-03-19"date; // -> "2015-03-19" date; // -> "2015-04-12"date; // -> "2015-04-12"date; // -> "2015-04-12" date; // -> "2016-03-12"date; // -> "2016-03-12"date; // -> "2016-03-12"
sub
NiceDate.sub(number, format)
Opposite of NiceDate.add.
Default Options
Options determine how date related strings are displayed, below are the default options.
months: "January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December" monthsShort: "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" days: "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" daysShort: "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" /** * Short representations of time measurements. * * @type */ indicators: year: "y" month: "m" week: "w" day: "d" hour: "h" minute: "m" second: "s" milliseconds: "ms" past: "ago" future: "in" now: "Just now"
License
MIT (http://www.opensource.org/licenses/mit-license.php)