A simple, light-weight model for date handling
npm install plain-dateA 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.
``js`
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.
`js`
import PlainDate, { startOfMonth, weekNumber } from 'plain-date'
#### Table of Contents
- PlainDate
- Properties
- Examples
- getNativeDate
- getComplexDate
- toString
- Parameters
- valueOf
- PlainDate/3_parameters
- Parameters
- Examples
- PlainDate/2_parameters
- Parameters
- Examples
- PlainDate/1_parameter
- Parameters
- Examples
- daysBetween
- Parameters
- startOfMonth
- Parameters
- endOfMonth
- Parameters
- startOfWeek
- Parameters
- endOfWeek
- Parameters
- weekNumber
- Parameters
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
#### Properties
- year number month
- number date
- number timestamp
- number
#### Examples
`javascript`
new PlainDate('2019-04-01') === new PlainDate('2019-04-01') // => true
`javascript`
new PlainDate('2019-04-01') < new PlainDate('2019-04-01') // => false
`javascript`
new PlainDate('2019-04-01') < new PlainDate('2019-04-11') // => true
`javascript`
new PlainDate('2019-04-01') > new PlainDate('2019-04-11') // => false
#### getNativeDate
Creates a new equivalent Date object.
Multiple calls yield multiple distinct objects.
Returns Date
#### getComplexDate
Returns Date
Meta
- deprecated: Use getNativeDate instead
#### toString
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.
##### Parameters
- format string (optional, default 'YYYY-MM[-DD]')
Returns string
#### valueOf
Enables fast comparison via < and >.
Returns number
#### Parameters
- year (number \| string) month
- (number \| string) date
- (number \| string)
#### Examples
`javascript`
new PlainDate(2019, 4, 1)
`javascript`
new PlainDate('2019', '04', '01')
#### Parameters
- year (number \| string) month
- (number \| string)
#### Examples
`javascript`
new PlainDate(2019, 4)
`javascript`
new PlainDate('2019', '4')
#### Parameters
- fullDate (string \| Date \| PlainDate | {year: number, month: number, date: number?})
#### Examples
`javascript`
new PlainDate('2019-04-01')
`javascript`
new PlainDate('2019-04')
`javascript`
new PlainDate(new Date(2019,3,1))
`javascript`
new PlainDate({
year: 2019,
month: 4,
})
#### Parameters
- one PlainDate other
- PlainDate
Returns number Difference between both dates, in days.
#### Parameters
- given PlainDate
Returns PlainDate The first of the same month.
#### Parameters
- given PlainDate
Returns PlainDate The last of the same month.
#### Parameters
- given PlainDate firstDayIndex
- number
Returns PlainDate The first of the same week.
#### Parameters
- given PlainDate firstDayIndex
- number
Returns PlainDate The last of the same week.
- See:
#### Parameters
- given` PlainDate
Returns number The number of the week within the current year, as per ISO 8601.