/
index.js
32 lines (29 loc) · 934 Bytes
/
index.js
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
import toDate from '../toDate/index.js'
/**
* @name setYear
* @category Year Helpers
* @summary Set the year to the given date.
*
* @description
* Set the year to the given date.
*
* @param {Date|String|Number} date - the date to be changed
* @param {Number} year - the year of the new date
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @returns {Date} the new date with the year setted
*
* @example
* // Set year 2013 to 1 September 2014:
* var result = setYear(new Date(2014, 8, 1), 2013)
* //=> Sun Sep 01 2013 00:00:00
*/
export default function setYear (dirtyDate, dirtyYear, dirtyOptions) {
var date = toDate(dirtyDate, dirtyOptions)
var year = Number(dirtyYear)
// Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
if (isNaN(date)) {
return new Date(NaN)
}
date.setFullYear(year)
return date
}