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
3 years ago
SIZE
671 B
Adds the given amount of time to a provided date object.
Install date-add as a package?
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
Files
impl.js
112 Lines(61 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

/**
 * @bit
 * @name dateAdd
 * @description 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.
 * @param {Object} original The date object
 * @param {number} increment The amount of time to add (or subtract if negative)
 * @param {string} unit (optional) The time unit to use. Defaults to milliseconds
 * @returns {Object} An updated date object
 * @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
 *
 */
module.exports = function dateAdd(original, increment, unit) {

	// Return undefiend if first argument isn't a Date object
	if (!(original instanceof Date)) {
		return(undefined);
	}

	switch(unit) {
		case 'seconds':
			// Add number of secodns to current date (ms*1000)
			var newDate = new Date(original);
			newDate.setTime(original.getTime() + (increment*1000));
			return newDate;
			break;
		case 'minutes':
			// Add number of minutes to current date (ms*1000*60)
			var newDate = new Date(original);
			newDate.setTime(original.getTime() + (increment*1000*60));
			return newDate;
			break;
		case 'hours':
			// Add number of hours to current date (ms*1000*60*60)
			var newDate = new Date(original);
			newDate.setTime(original.getTime() + (increment*1000*60*60));
			return newDate;
			break;
		case 'days':
			// Add number of days to current date
			var newDate = new Date(original);
			newDate.setDate(original.getDate() + increment);
			return newDate;
			break;
		case 'weeks':
			// Add number of weeks to current date
			var newDate = new Date(original);
			newDate.setDate(original.getDate() + (increment*7));
			return newDate;
			break;
		case 'months':
			// Get current date
			var oldDate = original.getDate();

			// Increment months (handles year rollover)
			var newDate = new Date(original);
			newDate.setMonth(original.getMonth() + increment);
			
			// If new day and old day aren't equal, set new day to last day of last month
			// (handles edge case when adding month to Jan 31st for example. Now goes to Feb 28th)
			if (newDate.getDate() != oldDate) {
				newDate.setDate(0);
			}

			// Handle leap years
			// If old date was Feb 29 (leap year) and new year isn't leap year, set new date to Feb 28
			if (original.getDate() == 29 && !isLeapYear(newDate.getFullYear())) {
				newDate.setMonth(1);
				newDate.setDate(28);
			}
			
			return newDate;
			break;
		case 'years':
			// Increment years
			var newDate = new Date(original);
			newDate.setFullYear(original.getFullYear() + increment);
			
			// Handle leap years
			// If old date was Feb 29 (leap year) and new year isn't leap year, set new date to Feb 28
			if (original.getDate() == 29 && !isLeapYear(newDate.getFullYear())) {
				newDate.setMonth(1);
				newDate.setDate(28);
			}
			
			return newDate;
			break;
		// Defaults to milliseconds
		default:
			var newDate = new Date(original);
			newDate.setTime(original.getTime() + increment);
			return newDate;
	}	
};

function isLeapYear(year) {
	return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}