What is date-arithmetic?
The date-arithmetic npm package provides utility functions for performing arithmetic operations on JavaScript Date objects. It allows you to add, subtract, and manipulate dates easily without dealing with the complexities of native Date methods.
What are date-arithmetic's main functionalities?
Add Time
This feature allows you to add a specified amount of time to a date. In this example, we add one week to the current date.
const dateArithmetic = require('date-arithmetic');
const now = new Date();
const nextWeek = dateArithmetic.add(now, 1, 'week');
console.log(nextWeek);
Subtract Time
This feature allows you to subtract a specified amount of time from a date. In this example, we subtract one month from the current date.
const dateArithmetic = require('date-arithmetic');
const now = new Date();
const lastMonth = dateArithmetic.subtract(now, 1, 'month');
console.log(lastMonth);
Difference Between Dates
This feature calculates the difference between two dates in a specified unit of time. In this example, we calculate the difference in days between January 1, 2023, and December 31, 2023.
const dateArithmetic = require('date-arithmetic');
const date1 = new Date(2023, 0, 1);
const date2 = new Date(2023, 11, 31);
const diff = dateArithmetic.diff(date1, date2, 'days');
console.log(diff);
Start of a Unit of Time
This feature returns the start of a specified unit of time for a given date. In this example, we get the start of the current month.
const dateArithmetic = require('date-arithmetic');
const now = new Date();
const startOfMonth = dateArithmetic.startOf(now, 'month');
console.log(startOfMonth);
End of a Unit of Time
This feature returns the end of a specified unit of time for a given date. In this example, we get the end of the current year.
const dateArithmetic = require('date-arithmetic');
const now = new Date();
const endOfYear = dateArithmetic.endOf(now, 'year');
console.log(endOfYear);
Other packages similar to date-arithmetic
moment
Moment.js is a widely-used library for parsing, validating, manipulating, and formatting dates in JavaScript. It offers a comprehensive set of features for date manipulation, similar to date-arithmetic, but also includes extensive formatting and parsing capabilities.
date-fns
date-fns provides a comprehensive set of functions for working with dates in JavaScript. It is modular, allowing you to import only the functions you need, which can result in smaller bundle sizes compared to Moment.js. date-fns offers similar arithmetic operations as date-arithmetic but with a more functional programming approach.
luxon
Luxon is a modern JavaScript library for working with dates and times. It is built on top of the native JavaScript Date and Intl APIs and provides a more modern and immutable API compared to Moment.js. Luxon offers similar date manipulation capabilities as date-arithmetic but with additional features like time zones and localization.
Date Arthmetic
A simple object containing some date math utils in the spirit of Moment.js. Unlike Moment this module, returns real date objects, so it isn't chainable.
var dateMath = require('date-arithmetic')
var date = dateMath.month(new Date)
API
all api methods return a new date. Date objects are never mutated.
Accessors
get and set date part values.
dateMath.milliseconds(date, [value])
dateMath.seconds(date, [value])
dateMath.minutes(date, [value])
dateMath.hours(date, [value])
dateMath.date(date, [value])
dateMath.day(date, [value])
dateMath.weekday(date, [value], [firstOfWeek = 0])
dateMath.month(date, [value])
dateMath.year(date, [value])
dateMath.decade(date, [value])
dateMath.century(date, [value])
startOf(data, unit, [firstOfWeek = 0])
return a new date with the relevent date parts zero'd out. You only need to provide a firstOfWeek
when the unit is 'week'
dateMath.startOf(new Date, 'day') // -> no time components
Valid unit values are; "second", "minutes", "hours", "day", "week", "month", "year", "decade", "century"
endOf(data, unit)
the opposite of startOf
dateMath.endOf(new Date, 'day') // -> one millisecond before tomorrow
Valid unit values are; "milliseconds", "second", "minutes", "hours", "day", "weekday", "month", "year", "decade", "century"
.
Math Functions
Arithmetic functions
dateMath.add(date, value, unit)
dateMath.subtract(date, value, unit)
dateMath.eq(dateA, dateB)
dateMath.neq(dateA, dateB)
dateMath.gte(dateA, dateB)
dateMath.gt(dateA, dateB)
dateMath.lte(dateA, dateB)
dateMath.lt(dateA, dateB)
dateMath.inRange(day, min, max, unit)
dateMath.min(dateA, dateB, dateN)
dateMath.max(dateA, dateB, dateN)
dateMath.diff(dateA, dateB, unit, asFloat)
Valid unit values are; "second", "minutes", "hours", "day", "week", "month", "year", "decade", "century"