Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Date objects in JavaScript without Time components

License

NotificationsYou 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

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 daybut the dates keep getting changed around because of timezones, this library avoidsthe hassel.

Status

This project is in beta. It is stable under the "happy path" usage but still needsto 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 is1970-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 froma number, string, or Date object
  • The month properties are indexed at 1, so the range is1-12 where 1 is January12 is December

Usage

under construction...

importDateOnlyfrom"@patient-otter/dateonly";constdt=newDateOnly()//1970-01-01dt.setDate(dt.getDate()+12)//1970-01-13dt.toJSON()//1970-01-13T:00:00.000ZconstdateWithTime=newDate();constdateWithouttime=DateOnly.fromDate(dateWithTime);constdateFromString=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])

ParamTypeDefaultDescription
[year]number1970The year the of the date
[date]number1The year the of the date
[month]number1The 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 ofDateOnly
Returns{number}:

dateOnly.getMonth()

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

Kind: instance method ofDateOnly
Returns{number}:

dateOnly.getDate()

Return the date value, or the day of the monthIndexed from 1, max of 31 depending the month

Kind: instance method ofDateOnly
Returns{number}:

dateOnly.setFullYear(v)

Return the year only from the DateOnly

Kind: instance method ofDateOnly

ParamTypeDescription
vnumberThe new desired value for the year

dateOnly.getDay()

Get day of the week

Kind: instance method ofDateOnly

dateOnly.setMonth(v)

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

Kind: instance method ofDateOnly

ParamTypeDescription
vnumberThe new desired value for the value

dateOnly.setDate(v)

Set the date value, or the day of the month

Kind: instance method ofDateOnly

ParamTypeDescription
vnumberThe new desired value for the date

dateOnly.toString([sep])

Set the date value, or the day of the month

Kind: instance method ofDateOnly
Returns{string}: the date represented as a string

ParamTypeDefaultDescription
[sep]string"-"The seperator to use between date components

dateOnly.toISOString()

Return a string in ISO8601 formatAll time values are set to zero

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

dateOnly.toJSON()

JSON serializes the date to an ISO8601 date

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

dateOnly.toDateString()

Returns a Date stirng

Kind: instance method ofDateOnly

dateOnly.isValid()

Returns if the current date time object is valid or not

Kind: instance method ofDateOnly

DateOnly.fromNumber(n)

Crete a DateOnly Object from a number, numbers represent the days since1 Jan 1970

Kind: static method ofDateOnly

ParamTypeDescription
nnumberThe 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 ofDateOnly

ParamTypeDescription
sstringThe string to read the date info from

DateOnly.fromDate(d)

Create a DateOnly Object from some string. Wraps new Date(str)

Kind: static method ofDateOnly

ParamTypeDescription
dDateThe 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 ofDateOnly
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

[8]ページ先頭

©2009-2025 Movatter.jp