timezone-date.ts
Enhancement of Date class with better timezone support.
Features
- Compliant to
Date
methods - Allow changing timezone anytime
- Typescript support
- Tiny code base (below 1KB minizipped)
Example
Jump between timezones:
import { TimezoneDate } from 'timezone-date.ts'
const d = TimezoneDate.fromDate(new Date('2020-04-21T10:00:00.000Z'))
d.timezone = 0
d.getHours()
d.timezone = +8
d.getHours()
d.getTimezoneOffset()
d.setHours(9)
d.timezone++
d.getHours()
d.toLocaleTimeString()
Set a specific time (e.g. from UI input)
let date = new TimezoneDate()
date.timezone = +8
date.setFullYear(2020, 11 - 1, 28)
date.setHours(9, 2, 38)
console.log(date.toString())
Installation
npm i timezone-date.ts
Construction
import { TimezoneDate } from 'timezone-date.ts'
new TimezoneDate(Date.now(), {timezone: +8})
new TimezoneDate()
new TimezoneDate(Date.now())
TimezoneDate.fromTime(Date.now())
TimezoneDate.fromDate(new Date())
TimezoneDate.from(Date.now())
License
This is free open sourced software (FOSS), with BSD 2-Clause License