datetimeSimple helper to help parse date strings into Date
, with additional functions.
UsageThe following symbols from
unicode LDML
are supported:
yyyy
- numeric year.
yy
- 2-digit year.
M
- numeric month.
MM
- 2-digit month.
d
- numeric day.
dd
- 2-digit day.
H
- numeric hour (0-23 hours).
HH
- 2-digit hour (00-23 hours).
h
- numeric hour (1-12 hours).
hh
- 2-digit hour (01-12 hours).
m
- numeric minute.
mm
- 2-digit minute.
s
- numeric second.
ss
- 2-digit second.
S
- 1-digit fractionalSecond.
SS
- 2-digit fractionalSecond.
SSS
- 3-digit fractionalSecond.
a
- dayPeriod, either AM
or PM
.
'foo'
- quoted literal.
./-
- unquoted literal.
Methods parseTakes an input string
and a formatString
to parse to a date
.
import { parse } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
parse ( "20-01-2019" , "dd-MM-yyyy" ) ;
parse ( "2019-01-20" , "yyyy-MM-dd" ) ;
parse ( "20.01.2019" , "dd.MM.yyyy" ) ;
parse ( "01-20-2019 16:34" , "MM-dd-yyyy HH:mm" ) ;
parse ( "01-20-2019 04:34 PM" , "MM-dd-yyyy hh:mm a" ) ;
parse ( "16:34 01-20-2019" , "HH:mm MM-dd-yyyy" ) ;
parse ( "01-20-2019 16:34:23.123" , "MM-dd-yyyy HH:mm:ss.SSS" ) ; Takes an input date
and a formatString
to format to a string
.
import { format } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
format ( new Date ( 2019 , 0 , 20 ) , "dd-MM-yyyy" ) ;
format ( new Date ( 2019 , 0 , 20 ) , "yyyy-MM-dd" ) ;
format ( new Date ( 2019 , 0 , 20 ) , "dd.MM.yyyy" ) ;
format ( new Date ( 2019 , 0 , 20 , 16 , 34 ) , "MM-dd-yyyy HH:mm" ) ;
format ( new Date ( 2019 , 0 , 20 , 16 , 34 ) , "MM-dd-yyyy hh:mm a" ) ;
format ( new Date ( 2019 , 0 , 20 , 16 , 34 ) , "HH:mm MM-dd-yyyy" ) ;
format ( new Date ( 2019 , 0 , 20 , 16 , 34 , 23 , 123 ) , "MM-dd-yyyy HH:mm:ss.SSS" ) ;
format ( new Date ( 2019 , 0 , 20 ) , "'today:' yyyy-MM-dd" ) ; dayOfYearReturns the number of the day in the year.
import { dayOfYear } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
dayOfYear ( new Date ( "2019-03-11T03:24:00" ) ) ; weekOfYearReturns the ISO week number of the provided date (1-53).
import { weekOfYear } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
weekOfYear ( new Date ( "2020-12-28T03:24:00" ) ) ; toIMFFormats the given date to IMF date time format. (Reference:
https://tools.ietf.org/html/rfc7231#section-7.1.1.1 )
import { toIMF } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
toIMF ( new Date ( 0 ) ) ; isLeapReturns true if the given date or year (in number) is a leap year. Returns false
otherwise.
import { isLeap } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
isLeap ( new Date ( "1970-01-01" ) ) ;
isLeap ( new Date ( "1972-01-01" ) ) ;
isLeap ( new Date ( "2000-01-01" ) ) ;
isLeap ( new Date ( "2100-01-01" ) ) ;
isLeap ( 1972 ) ; differenceReturns the difference of the 2 given dates in the given units. If the units are
omitted, it returns the difference in the all available units.
Available units: "milliseconds", "seconds", "minutes", "hours", "days", "weeks",
"months", "quarters", "years"
import { difference } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
const date0 = new Date ( "2018-05-14" ) ;
const date1 = new Date ( "2020-05-13" ) ;
difference ( date0, date1, { units: [ "days" , "months" , "years" ] } ) ;
difference ( date0, date1) ;
Constants SECONDimport { SECOND } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
console . log ( SECOND ) ; MINUTEimport { MINUTE } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
console . log ( MINUTE ) ; HOURimport { HOUR } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
console . log ( HOUR ) ; DAYimport { DAY } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
console . log ( DAY ) ; WEEKimport { WEEK } from "https://deno.land/std@0.116.0/datetime/mod.ts" ;
console . log ( WEEK ) ;