镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
alexpuschPublic

About

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

Resources

Stars

111 stars

Watchers

3 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

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

Stars

111 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages