date-extended
date-extended
is a Javascript library that can be used standalone or incorporated into extended
var date = require("date-extended");
Or
var myextended = require("extended")
.register(require("date-extended"));
Installation
npm install date-extended
Or download the source (minified)
Usage
getDaysInMonth
Returns the number of days in the month of a date
date.getDaysInMonth(new Date(2006, 1, 1));
date.getDaysInMonth(new Date(2004, 1, 1));
date.getDaysInMonth(new Date(2006, 2, 1));
date.getDaysInMonth(new Date(2006, 3, 1));
date(new Date(2006, 4, 1)).getDaysInMonth().value();
date(new Date(2006, 5, 1)).getDaysInMonth().value();
date(new Date(2006, 6, 1)).getDaysInMonth();
isLeapYear
Determines if a date is a leap year
date.isLeapYear(new Date(1600, 0, 1));
date.isLeapYear(new Date(2006, 0, 1));
date.isLeapYear(new Date(2004, 0, 1));
date(new Date(1900, 0, 1)).isLeapYear();
date(new Date(2000, 0, 1)).isLeapYear();
date(new Date(1800, 0, 1)).isLeapYear();
isWeekend
Determines if a date is on a weekend
var thursday = new Date(2006, 8, 21);
date.isWeekend(thursday));
var saturday = new Date(2006, 8, 23);
date(saturday).isWeekend();
var sunday = new Date(2006, 8, 24);
date.isWeekend(sunday);
var monday = new Date(2006, 8, 25);
date(monday).isWeekend());
getTimezoneName
Get the timezone of a date
compare
Compares two dates
var d1 = new Date();
d1.setHours(0);
date.compare(d1, d1);
var d1 = new Date(), d2 = new Date();
d1.setHours(0);
d2.setFullYear(2005);
d2.setHours(12);
date.compare(d1, d2, "date");
date(d1).compare(d2, "datetime");
var d1 = new Date(), d2 = new Date();
d1.setHours(0);
d2.setFullYear(2005);
d2.setHours(12);
date(d2).compare(d1, "date");
date(d1).compare(d2, "time");
add
Adds a specified interval and amount to a date
- day | days
- weekday | weekdays
- year | years
- week | weeks
- quarter | quarters
- months | months
- hour | hours
- minute | minutes
- second | seconds
- millisecond | milliseconds
var dtA = new Date(2005, 11, 27);
date.add(dtA, "year", 1);
date(dtA).add("years", 1).value();
dtA = new Date(2000, 0, 1);
date.add(dtA, "quarter", 1);
date(dtA).add("quarters", 1).value();
dtA = new Date(2000, 0, 1);
date.add(dtA, "month", 1);
date(dtA).add("months", 1).value();
dtA = new Date(2000, 0, 31);
date.add(dtA, "month", 1);
date(dtA).add("months", 1).value();
dtA = new Date(2000, 0, 1);
date.add(dtA, "week", 1);
date(dtA).add("weeks", 1).value();
dtA = new Date(2000, 0, 1);
date(dtA).add("day", 1).value();
dtA = new Date(2000, 0, 1);
date(dtA).add("weekday", 1);
dtA = new Date(2000, 0, 1, 11);
date(dtA).add("hour", 1).value();
dtA = new Date(2000, 11, 31, 23, 59);
date.add(dtA, "minute", 1);
dtA = new Date(2000, 11, 31, 23, 59, 59);
date.add(dtA, "second", 1);
dtA = new Date(2000, 11, 31, 23, 59, 59, 999);
date.add(dtA, "millisecond", 1);
difference
Finds the difference between two dates based on the specified interval
- day | days
- weekday | weekdays
- year | years
- week | weeks
- quarter | quarters
- months | months
- hour | hours
- minute | minutes
- second | seconds
- millisecond | milliseconds
var dtA, dtB;
dtA = new Date(2005, 11, 27);
dtB = new Date(2006, 11, 27);
date.difference(dtA, dtB, "year");
dtA = new Date(2000, 1, 29);
dtB = new Date(2001, 2, 1);
date.difference(dtA, dtB, "quarter");
date(dtA).difference(dtB, "month").value();
dtA = new Date(2000, 1, 1);
dtB = new Date(2000, 1, 8);
date.difference(dtA, dtB, "week");
dtA = new Date(2000, 1, 29);
dtB = new Date(2000, 2, 1);
date(dtA).difference(dtB, "day").value();
dtA = new Date(2006, 7, 3);
dtB = new Date(2006, 7, 11);
date.difference(dtA, dtB, "weekday");
dtA = new Date(2000, 11, 31, 23);
dtB = new Date(2001, 0, 1, 0);
date(dtA).difference(dtB, "hour").value();
dtA = new Date(2000, 11, 31, 23, 59);
dtB = new Date(2001, 0, 1, 0, 0);
date.difference(dtA, dtB, "minute");
dtA = new Date(2000, 11, 31, 23, 59, 59);
dtB = new Date(2001, 0, 1, 0, 0, 0);
date(dtA).difference(dtB, "second").value();
dtA = new Date(2000, 11, 31, 23, 59, 59, 999);
dtB = new Date(2001, 0, 1, 0, 0, 0, 0);
date.difference(dtA, dtB, "millisecond");
format
Formats a date to the specified format string
G
Era designator Text ADy
Year Year 1996; 96M
Month in year Month July; Jul; 07w
Week in year Number 27W
Week in month Number 2D
Day in year Number 189d
Day in month Number 10E
Day in week Text Tuesday; Tuea
Am/pm marker Text PMH
Hour in day (0-23) Number 0k
Hour in day (1-24) Number 24K
Hour in am/pm (0-11) Number 0h
Hour in am/pm (1-12) Number 12m
Minute in hour Number 30s
Second in minute Number 55S
Millisecond Number 978z
Time zone General time zone Pacific Standard Time; PST; GMT-08:00Z
Time zone RFC 822 time zone -0800
var date = new Date(2006, 7, 11, 0, 55, 12, 345);
date.format(date, "EEEE, MMMM dd, yyyy");
date(date).format("M/dd/yy").value();
date.format(date, "E");
date(date).format("h:m a").value();
date.format(date, 'h:m:s');
date(date).format('h:m:s.SS').value();
date.format(date, 'k:m:s.SS');
date(date).format('H:m:s.SS').value();
date.format(date, "ddMMyyyy");
parseDate
Parses a date string into a date object
G
Era designator Text ADy
Year Year 1996; 96M
Month in year Month July; Jul; 07w
Week in year Number 27W
Week in month Number 2D
Day in year Number 189d
Day in month Number 10E
Day in week Text Tuesday; Tuea
Am/pm marker Text PMH
Hour in day (0-23) Number 0k
Hour in day (1-24) Number 24K
Hour in am/pm (0-11) Number 0h
Hour in am/pm (1-12) Number 12m
Minute in hour Number 30s
Second in minute Number 55S
Millisecond Number 978z
Time zone General time zone Pacific Standard Time; PST; GMT-08:00Z
Time zone RFC 822 time zone -0800
var aug_11_2006 = new Date(2006, 7, 11, 0);
date.parse("08/11/06", "MM/dd/yy");
date.parse("11Aug2006", 'ddMMMyyyy');
date.parse("Aug2006", 'MMMyyyy');
date.parse("Aug 11, 2006", "MMM dd, yyyy");
date.parse("August 11, 2006", "MMMM dd, yyyy");
date.parse("Friday, August 11, 2006", "EEEE, MMMM dd, yyyy");
FromNow and Ago
The following are convenience methods for adding ad subtracting intervals from the current date.
yearsFromNow
yearsAgo
monthsFromNow
monthsAgo
daysFromNow
daysAgo
hoursFromNow
hoursAgo
minutesFromNow
minutesAgo
secondsFromNow
secondsAgo
date(2).yearsAgo();
date.yearsAgo(2)
date(5).hoursFromNow();
date.hoursFromNow(5);
date(2).secondsAgo();
date.secondsAgo(2)