A module that adds new methods to the native Date class.
npm install node-date-utilities
In order to use the methods, you only need to import the package and the everything will be available.
require('node-date-utilities');
or
import 'node-date-utilities';
Checks if a specific year is a leap year
- year: {number} the year to be checked
Date.isLeapYear(1996) // true
Date.isLeapYear(1995) // false
Returns the number of days of a month in a specific year
- year: {number} the year to be considered
- month: {number} The month whose days will be counted
Date.getDaysInMonth(1996, 0) // 31
Date.getDaysInMonth(1996, 1) // 29
Returns a Date instance of the next day
const tomorrow = Date.tomorrow()
Returns a Date instance of the previous day
const yesterday = Date.yesterday()
Check if two dates are the same day
- date1: {Date} the first date
- date2: {Date} the second date
const date1 = new Date("1995-02-17T03:24:00");
const date2 = new Date("1995-02-17T12:24:00");
Date.sameDay(date1, date2) // true
Adds a specific number of days/months/years to the current Date instance
- options: AddSubOptions values to be added/subtracted
{
years: 1,
months: 1,
days: 1
}
const date = new Date("1995-02-17T03:24:00");
date.add({days: 1}); // 1995-02-18T03:24:00
date.add({months: 1}); // 1995-03-18T03:24:00
date.add({years: 1}); // 1996-01-18T03:24:00
Subtracts a specific number of days/months/years to the current Date instance
- options: AddSubOptions values to be added/subtracted
{
years: 1,
months: 1,
days: 1
}
const date = new Date("1995-02-17T03:24:00");
date.subtract({days: -1}); // 1995-02-16T03:24:00
date.subtract({months: -1}); // 1995-01-18T03:24:00
date.add({years: -1}); // 1994-01-18T03:24:00
Checks if the current Date instance is a leap year
const date = new Date("1995-02-17T03:24:00");
date.isLeapYear(); // false
Removes the time part from a Date instance
const date = new Date("1995-02-17T03:24:00");
date.clearTime(); // 1995-02-17T00:00:00.0000
Returns the number of days of the instance month
const date = new Date("1995-01-17T03:24:00");
date.getDaysInMonth(); // 31
Clones the current Date instance
const date = new Date("1995-01-17T03:24:00");
const clone = date.clone();
npm run test