Giter Site home page Giter Site logo

dotago.js's Introduction

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

dotago.js's People

Contributors

alexpusch avatar knpwrs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.