-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
40 lines (33 loc) · 1.13 KB
/
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
33
34
35
36
37
38
39
40
const SECONDS_IN_DAY = 24 * 60 * 60;
const MISSING_LEAP_YEAR_DAY = SECONDS_IN_DAY * 1000;
const MAGIC_NUMBER_OF_DAYS = (25567 + 2);
const isDate = date => Object.prototype.toString.call(date) === "[object Date]" && !isNaN(date.getTime())
/**
* JavaScript dates can be constructed by passing milliseconds
* since the Unix epoch (January 1, 1970) example: new Date(12312512312);
* 1. Subtract number of days between:
* Jan 1, 1900 and Jan 1, 1970, plus 2 ("excel leap year bug")
* 2. Convert to milliseconds.
*
* @method getJsDateFromExcel
* @param {Number} excelDate
* @return {Date}
*/
module.exports.getJsDateFromExcel = (excelDate) => {
if (!Number(excelDate)) {
throw new Error('wrong input format')
}
const delta = excelDate - MAGIC_NUMBER_OF_DAYS;
const parsed = delta * MISSING_LEAP_YEAR_DAY;
const date = new Date(parsed)
if (!isDate(date)) {
throw new Error('wrong excel date input')
}
return date
}
module.exports.getExcelDateFromJs = (date) => {
if (!isDate(date)) {
throw new Error('wrong input format')
}
return (date.getTime() / MISSING_LEAP_YEAR_DAY) + MAGIC_NUMBER_OF_DAYS
}