https://github.com/beenotung/timezone-date
Enhancement of Date class with better timezone support.
https://github.com/beenotung/timezone-date
browser-compatibility date nodejs timezone typescript
Last synced: 3 months ago
JSON representation
Enhancement of Date class with better timezone support.
- Host: GitHub
- URL: https://github.com/beenotung/timezone-date
- Owner: beenotung
- License: bsd-2-clause
- Created: 2020-04-24T03:14:47.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-05-07T14:51:37.000Z (about 3 years ago)
- Last Synced: 2025-01-01T05:42:00.413Z (5 months ago)
- Topics: browser-compatibility, date, nodejs, timezone, typescript
- Language: TypeScript
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# timezone-date.ts
Enhancement of Date class with better timezone support.
[](https://www.npmjs.com/package/timezone-date.ts)
[](https://bundlephobia.com/package/timezone-date.ts)
[](https://bundlephobia.com/package/timezone-date.ts)## Features
- Compliant to `Date` methods
- Allow changing timezone anytime
- Typescript support
- Tiny code base (below 1KB minizipped)## Example
Jump between timezones:
```typescript
import { TimezoneDate } from 'timezone-date.ts'const d = TimezoneDate.fromDate(new Date('2020-04-21T10:00:00.000Z'))
d.timezone = 0
d.getHours() // 10d.timezone = +8
d.getHours() // 18d.getTimezoneOffset() // -480 (in mintes, same format as Native Date)
d.setHours(9)
d.timezone++
d.getHours() // 10
d.toLocaleTimeString() // '10:00:00 AM'
```Set a specific time (e.g. from UI input)
```typescript
let date = new TimezoneDate()
date.timezone = +8
date.setFullYear(2020, 11 - 1, 28)
date.setHours(9, 2, 38)
console.log(date.toString()) // Sat Nov 28 2020 09:02:38 GMT+0800 (Hong Kong Standard Time)
```## Installation
```bash
npm i timezone-date.ts
```
## Construction
```typescript
import { TimezoneDate } from 'timezone-date.ts'/* custom timezone */
new TimezoneDate(Date.now(), {timezone: +8})/* default timezone (in the environment) */
new TimezoneDate() // current time
new TimezoneDate(Date.now())
TimezoneDate.fromTime(Date.now())
TimezoneDate.fromDate(new Date())
TimezoneDate.from(Date.now()) // from Date or timestamp
```## License
This is free open sourced software (FOSS), with [BSD 2-Clause License](./LICENSE)