jquense / date-math

simple date math module
MIT License
54 stars 12 forks source link

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.

import * as dateMath from '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.

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".

Math Functions

Arithmetic functions

Valid unit values are; "seconds", "minutes", "hours", "day", "week", "month", "year", "decade", "century"