Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

date-add

v0.0.6arrow_drop_down
v0.0.6
v0.0.5
v0.0.4
v0.0.3
v0.0.2
v0.0.1
STATUS
Passing
DOWNLOADS
38
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
2 years ago
SIZE
671 B
subject
Adds the given amount of time to a provided date object.
Use date-add in your project ?
Copied
npm i @bit/gideonshils.js-methods.time.date-add
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Component Example
React
React
Vue
Angular
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';
revert changes
reload
toggle layout
maximize
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
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
```js
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