Skip to content

Silly syntactic sugar for creating relative dates and time durations with Javascript

License

Notifications You must be signed in to change notification settings

alexpusch/dotago.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

API

  • 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
  • ago
  • fromNow
  • asDate

About

Silly syntactic sugar for creating relative dates and time durations with Javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published