23
GitHub - alexpusch/dotago.js: Silly syntactic sugar for creating relative dates...
source link: https://github.com/alexpusch/dotago.js
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
dotago.js
Silly syntactic sugar for creating relative dates and time durations with Javascript
Work with dates:
// now: 2020-06-18T10:00:00Z 1..minute.ago.asDate // 2020-06-18T09:59:00Z 2..hours.ago.asDate // 2020-06-18T08:00:00Z 3..hours.fromNow.asDate // 2020-06-18T13:00:00Z
Work with timestamps:
// now: 2020-06-18T10:00:00Z 1..minute.ago // 1592474340000 1..hour.ago // 1592470800000 1..hour.fromNow // 1592478000000
Just get the duration in milliseconds:
1..second // 1000 1..minute // 6000 1..hour // 3600000
Do math
// now: 2020-06-18T10:00:00Z (1..hour.ago + 2..minutes).asDate // 2020-06-18T09:02:00Z
How does it work?
Our beloved javascript allows us to run methods on primitive numbers. The thing is that 1.foo()
is parsed as an illegal Float number.
1.
translates into 1.0
and so 1..foo()
translates into 1.0.foo()
which is totally legal Javascript.
Usage
npm i --save dotago
require('dotago').load(); console.log(2..hours.fromNow.asDate)
- load() - monkeypatch Number.prototype with dotago methods
- unload() - remove dotago methods from Number.prototype
We add the following getter methods to Number.prototype
- second/seconds
- minute/minutes
- hour/hours
- day/days
- week/weeks
- fromNow
- asDate
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK