Skip to content

Date objects in JavaScript without Time components

License

Notifications You must be signed in to change notification settings

lwileczek/dateonly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dateonly

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.

Why?

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.

Status

This project is in beta. It is stable under the "happy path" usage but still needs to have added validation around inputs.

Installation

npm install @patient-otter/dateonly

Differences with Date Objects

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

Usage

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'

API

Classes

Create a new DateOnly object
DateOnly

A date object with without time

Create a new DateOnly object

Kind: global class

new Create a new DateOnly object([year], [date], [month])

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

DateOnly

A date object with without time

Kind: global class

dateOnly.getFullYear()

Return the year only from the DateOnly

Kind: instance method of DateOnly
Returns{number}:

dateOnly.getMonth()

Return the month value from the DateOnly. Indexed from 1, 12 is december

Kind: instance method of DateOnly
Returns{number}:

dateOnly.getDate()

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}:

dateOnly.setFullYear(v)

Return the year only from the DateOnly

Kind: instance method of DateOnly

Param Type Description
v number The new desired value for the year

dateOnly.getDay()

Get day of the week

Kind: instance method of DateOnly

dateOnly.setMonth(v)

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

dateOnly.setDate(v)

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

dateOnly.toString([sep])

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

dateOnly.toISOString()

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

dateOnly.toJSON()

JSON serializes the date to an ISO8601 date

Kind: instance method of DateOnly
Returns{string}: the date represented as an ISO8601 string

dateOnly.toDateString()

Returns a Date stirng

Kind: instance method of DateOnly

dateOnly.isValid()

Returns if the current date time object is valid or not

Kind: instance method of DateOnly

DateOnly.fromNumber(n)

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

DateOnly.fromString(s)

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

DateOnly.fromDate(d)

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

DateOnly.now()

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

About

Date objects in JavaScript without Time components

Resources

License

Stars

Watchers

Forks

Packages

No packages published