A simple, light-weight model for date handling in JavaScript
The package includes both an ESM module (browser-compatible) and CJS modules (npm-compatible).
plain-date
is written in node-style and converted to ESM for wider distribution. The default is therefore the node variant.
require('plain-date')
gives you the PlainDate class.
require('plain-date/calculation')
gives you helper functions.
const PlainDate = require('plain-date')
const { startOfMonth, weekNumber } = require('plain-date/calculation')
index.mjs
yields the PlainDate
class as its default export and
the helper functions as named exports.
import PlainDate, { startOfMonth, weekNumber } from 'plain-date'
- PlainDate
- PlainDate/3_parameters
- PlainDate/2_parameters
- PlainDate/1_parameter
- daysBetween
- startOfMonth
- endOfMonth
- startOfWeek
- endOfWeek
- weekNumber
PlainDate objects represent calendar dates or months. They focus on ease of use and are very light in comparison with JavaScript's native Date type.
They have a small set of properties, are immutable, and every date can only ever exist once.
If you try to create a new PlainDate object with a date that has previously been created, you will receive that pre-existing PlainDate object.
Additionally, PlainDate objects can be intuitively compared.
PlainDate objects can be constructed with one to three parameters. Behaviour for unexpected inputs is not defined. In order to stay light-weight, there is no intelligent type checking.
Type: Object
new PlainDate('2019-04-01') === new PlainDate('2019-04-01') // => true
new PlainDate('2019-04-01') < new PlainDate('2019-04-01') // => false
new PlainDate('2019-04-01') < new PlainDate('2019-04-11') // => true
new PlainDate('2019-04-01') > new PlainDate('2019-04-11') // => false
Creates a new equivalent Date
object.
Multiple calls yield multiple distinct objects.
Returns Date
Returns Date
Meta
- deprecated: Use
getNativeDate
instead
Replaces each token in the given string by the appropriate string representation of the related data:
- YYYY: full year-number.
- YY: last two digits of year.
- MM: zero-padded two-digit month.
- DD: zero-padded two-digit date.
Defaults to the ISO format.
format
string (optional, default'YYYY-MM[-DD]'
)
Returns string
Enables fast comparison via <
and >
.
Returns number
new PlainDate(2019, 4, 1)
new PlainDate('2019', '04', '01')
new PlainDate(2019, 4)
new PlainDate('2019', '4')
new PlainDate('2019-04-01')
new PlainDate('2019-04')
new PlainDate(new Date(2019,3,1))
new PlainDate({
year: 2019,
month: 4,
})
Returns number Difference between both dates, in days.
given
PlainDate
Returns PlainDate The first of the same month.
given
PlainDate
Returns PlainDate The last of the same month.
Returns PlainDate The first of the same week.
Returns PlainDate The last of the same week.
given
PlainDate
Returns number The number of the week within the current year, as per ISO 8601.