date-add

v0.0.2arrow_drop_down
v0.0.2
v0.0.1
STATUS
Passing
DOWNLOADS
0
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
7 years ago
SIZE
691 B
Adds the given amount of time to a provided date object.
1 contributor
Install date-add as a package?
Copied
npm i @bit/tomlandau.simple-js.time.date-add
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.cloud
Component Example
React
React
Vue
Angular
React Native
Add dependency... help_outline
Just
import
any of the 1 million components
and packages in Bit or NPM to the example.
import Button from '@bit/grommet.grommet.button';
import Lodash from 'lodash';
toggle layout
No preview available
modifieddraft
chevron_left
chevron_right
dateAdd (
original:Object,
increment:number,
unit:string
) : Object

Adds the given amount of time to a provided date object. Day, week, month, and year increments maintain the same hour for changes that pass through daylight saving time.

Example
var originalDate = new Date('July 1, 2016 18:45:10');

dateAdd(originalDate, 6000, 'milliseconds');  // => 'July 1, 2016 18:45:16'
dateAdd(originalDate, 5, 'seconds');          // => 'July 1, 2016 18:45:15'
dateAdd(originalDate, 5, 'minutes');          // => 'July 1, 2016 18:45:10'
dateAdd(originalDate, 5, 'hours');            // => 'July 1, 2016 23:45:10'
dateAdd(originalDate, 5, 'days');             // => 'July 6, 2016 18:45:10'
dateAdd(originalDate, 2, 'weeks');            // => 'July 15, 2016 18:45:10'
dateAdd(originalDate, 2, 'months');           // => 'September 1, 2016 18:45:10'
dateAdd(originalDate, 5, 'years');            // => 'July 1, 2021 18:45:10'
dateAdd(originalDate, -1, 'days');            // => 'June 30, 2016 18:45:16'

dateAdd(originalDate, 6000);                  // => 'July 1, 2016 18:45:16' - Defaults to ms
Arguments
original: Object

The date object

increment: number

The amount of time to add (or subtract if negative)

unit: string

(optional) The time unit to use. Defaults to milliseconds

Returns
Object

An updated date object

Test Summary

dateAdd() should return a date in the future when current date is passed with a positive increment

Pass

dateAdd() should return a date in the past when current date is passed with a negative increment

Pass

dateAdd() should return an identical date when the increment is 0

Pass

dateAdd() should default to milliseconds when no time unit is specified

Pass

dateAdd() should default to milliseconds when an unsupported time unit is specified

Pass

dateAdd() should retain the time but increment the date when passing through daylight saving time for units of days and above

Pass

dateAdd() should return undefined when the first argument is not a Date object

Pass

dateAdd() should properly handle leap years when incrementing Feb 29th date to non leap year

Pass

dateAdd() should properly handle leap years when incrementing Feb 29th date to other leap year

Pass

dateAdd() should revert to last day of month when incrementing to month that doesn’t contain current day

Pass
Help and resources