Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
date-arithmetic
Advanced tools
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.
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);
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 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 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.
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.
import * as dateMath from 'date-arithmetic'
var date = dateMath.month(new Date)
all api methods return a new date. Date objects are never mutated.
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; "seconds", "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", "seconds", "minutes", "hours", "day", "weekday", "month", "year", "decade", "century"
.
Arithmetic functions
dateMath.add(date, value, unit)
dateMath.subtract(date, value, unit)
dateMath.eq(dateA, dateB, [unit])
dateMath.neq(dateA, dateB, [unit])
dateMath.gte(dateA, dateB, [unit])
dateMath.gt(dateA, dateB, [unit])
dateMath.lte(dateA, dateB, [unit])
dateMath.lt(dateA, dateB, [unit])
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; "seconds", "minutes", "hours", "day", "week", "month", "year", "decade", "century"
FAQs
simple date math util
The npm package date-arithmetic receives a total of 267,472 weekly downloads. As such, date-arithmetic popularity was classified as popular.
We found that date-arithmetic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.