power-dates
v1.2.0
Published
Extend the functionality of Date objects to make life a little easier
Downloads
1
Readme
Power Dates
Extend the capabilities of the Date object in JavaScript.
Installation
npm install power-dates
TimeDelta
Usage
const { TimeDelta } = require('power-dates');
// or
import { TimeDelta } from 'power-dates';
// default to current date and time
const delta = new TimeDelta();
// or
const date = new Date();
const delta = new TimeDelta(date);
Methods
// get the distance between two dates
const delta = new TimeDelta(new Date('2020-01-01'));
const delta.delta(); // checks days between now and 2020-01-01
const delta.createDelta(new Date()); // checks days between 2020-01-01 and whatever date is passed in
TimeDelta.delta Arguments
| Argument | Type | Description | Default | | --- | --- | --- | --- | | type | String (minutes, hours, days, weeks, months, years) | What time setting to use | days | | round | Boolean | Whether to round the number or not | true |
TimeDelta.createDelta Arguments
| Argument | Type | Description | Default | | --- | --- | --- | --- | | date | Date | The date to compare to | required | | type | String (minutes, hours, days, weeks, months, years) | What time setting to use | days | | round | Boolean | Whether to round the number or not | true |
Examples
const delta = new TimeDelta(new Date('2020-01-01'));
delta.delta(); // days between now and 2020-01-01
const delta = new TimeDelta(new Date('2020-01-01'));
delta.delta('hours'); // hours between now and 2020-01-01
const delta = new TimeDelta(new Date('2020-01-01'));
delta.delta('minutes', false); // minutes between now and 2020-01-01 in a double
const delta = new TimeDelta(new Date('2020-01-01'));
delta.createDelta(new Date('2020-01-02')); // days between 2020-01-01 and 2020-01-02
const delta = new TimeDelta(new Date('2020-01-01'));
delta.createDelta(new Date('2020-01-02'), 'hours'); // hours between 2020-01-01 and 2020-01-02
const delta = new TimeDelta(new Date('2020-01-01'));
delta.createDelta(new Date('2020-01-02'), 'minutes', false); // minutes between 2020-01-01 and 2020-01-02 in a double