diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 2cca31c63..fa02290f3 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -64,7 +64,8 @@ var utils = module.exports = { return 25569 + (d.getTime() / (24 * 3600 * 1000)) - (date1904 ? 1462 : 0); }, excelToDate: function(v, date1904) { - return new Date((v - 25569 + (date1904 ? 1462 : 0)) * 24 * 3600 * 1000); + var millisecondSinceEpoch = Math.round((v - 25569 + (date1904 ? 1462 : 0)) * 24 * 3600 * 1000); + return new Date(millisecondSinceEpoch); }, parsePath: function(filepath) { var last = filepath.lastIndexOf('/'); diff --git a/spec/unit/utils/utils.spec.js b/spec/unit/utils/utils.spec.js index c23cf3b1c..a108217b9 100644 --- a/spec/unit/utils/utils.spec.js +++ b/spec/unit/utils/utils.spec.js @@ -39,4 +39,33 @@ describe('utils', function() { }); }); }); + + describe('dateToExcel', function() { + it('should convert date to excel properly', function() { + const myDate = new Date(Date.UTC(2017, 11, 15, 17, 0, 0, 0)); + + const excelDate = utils.dateToExcel(myDate, false); + + expect(excelDate).to.equal(43084.70833333333); + }); + }); + + describe('excelToDate', function() { + it('should round to the nearest millisecond when parsing excel date', function() { + const myDate = new Date(Date.UTC(2017, 11, 15, 17, 0, 0, 0)); + const excelDate = utils.dateToExcel(myDate, false); + + const dateConverted = utils.excelToDate(excelDate, false); + + expect(dateConverted).to.deep.equal(myDate); + }); + it('should not lost millisecond precision when parsing excel date', function() { + const myDate = new Date(Date.UTC(2017, 11, 15, 17, 0, 0, 0)); + const excelDate = utils.dateToExcel(myDate, false); + + const dateConverted = utils.excelToDate(excelDate, false); + + expect(dateConverted).to.deep.equal(myDate); + }); + }); });