A drop in replacement for Date objects in JavaScript without a time component. This avoids any hassel having to do with timezones.
Approximately 2kb after being built and minified.
If you find yourself only caring about dates, like a birthday, holiday, or vacation day but the dates keep getting changed around because of timezones, this library avoids the hassel.
This project is in beta. It is stable under the "happy path" usage but still needs to have added validation around inputs.
npm install @patient-otter/dateonly
There are some differences to with built in Date objects
- The default date is
1970-01-01
not the current day. e.g.new DateOnly().toString() // 1970-01-01
- The Constructor only takes numbers, you must use static methods to create from a number, string, or Date object
- The month properties are indexed at 1, so the range is
1-12
where 1 is January 12 is December
under construction...
import DateOnly from "@patient-otter/dateonly";
const dt = new DateOnly() //1970-01-01
dt.setDate(dt.getDate() + 12)
//1970-01-13
dt.toJSON() //1970-01-13T:00:00.000Z
const dateWithTime = new Date();
const dateWithouttime = DateOnly.fromDate(dateWithTime);
const dateFromString = DateOnly.fromString('06/25/2030');
console.log(dateFromString.toString()) //'2030-06-25'
- Create a new DateOnly object
- DateOnly
A date object with without time
Param | Type | Default | Description |
---|---|---|---|
[year] | number |
1970 |
The year the of the date |
[date] | number |
1 |
The year the of the date |
[month] | number |
1 |
The year the of the date |
A date object with without time
Kind: global class
Return the year only from the DateOnly
Kind: instance method of DateOnly
Returns{number}:
Return the month value from the DateOnly. Indexed from 1, 12 is december
Kind: instance method of DateOnly
Returns{number}:
Return the date value, or the day of the month Indexed from 1, max of 31 depending the month
Kind: instance method of DateOnly
Returns{number}:
Return the year only from the DateOnly
Kind: instance method of DateOnly
Param | Type | Description |
---|---|---|
v | number |
The new desired value for the year |
Get day of the week
Kind: instance method of DateOnly
Return the month value from the DateOnly. Indexed from 1, 12 is december
Kind: instance method of DateOnly
Param | Type | Description |
---|---|---|
v | number |
The new desired value for the value |
Set the date value, or the day of the month
Kind: instance method of DateOnly
Param | Type | Description |
---|---|---|
v | number |
The new desired value for the date |
Set the date value, or the day of the month
Kind: instance method of DateOnly
Returns{string}: the date represented as a string
Param | Type | Default | Description |
---|---|---|---|
[sep] | string |
"-" |
The seperator to use between date components |
Return a string in ISO8601 format All time values are set to zero
Kind: instance method of DateOnly
Returns{string}: the date represented as an ISO8601 string
JSON serializes the date to an ISO8601 date
Kind: instance method of DateOnly
Returns{string}: the date represented as an ISO8601 string
Returns a Date stirng
Kind: instance method of DateOnly
Returns if the current date time object is valid or not
Kind: instance method of DateOnly
Crete a DateOnly Object from a number, numbers represent the days since 1 Jan 1970
Kind: static method of DateOnly
Param | Type | Description |
---|---|---|
n | number |
The number to be converted to a DateOnly Object |
Create a DateOnly Object from some string. Wraps new Date(str)
Kind: static method of DateOnly
Param | Type | Description |
---|---|---|
s | string |
The string to read the date info from |
Create a DateOnly Object from some string. Wraps new Date(str)
Kind: static method of DateOnly
Param | Type | Description |
---|---|---|
d | Date |
The date to use in creating a new DateOnly object using the date's UTC values |
Create a new DateOnly object for the current date
Kind: static method of DateOnly
Returns{dateonly}: A donly only object with the current local date