• Stars
    star
    108
  • Rank 314,436 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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