diff --git a/source/lib/utils.js b/source/lib/utils.js index 8830147d..ffd68a8b 100644 --- a/source/lib/utils.js +++ b/source/lib/utils.js @@ -140,14 +140,14 @@ let getExcelRowCol = (str) => { let getExcelTS = (date) => { let thisDt = new Date(date); - thisDt.setDate(thisDt.getDate() + 1); + thisDt = new Date(thisDt.getTime() + 24 * 60 * 60 * 1000); let epoch = new Date('1900-01-01T00:00:00.0000Z'); // Handle legacy leap year offset as described in §18.17.4.1 const legacyLeapDate = new Date('1900-02-28T23:59:59.999Z'); if (thisDt - legacyLeapDate > 0) { - thisDt.setDate(thisDt.getDate() + 1); + thisDt = new Date(thisDt.getTime() + 24 * 60 * 60 * 1000); } // Get milliseconds between date sent to function and epoch diff --git a/tests/library.test.js b/tests/library.test.js index b818e991..d2acba1b 100644 --- a/tests/library.test.js +++ b/tests/library.test.js @@ -31,6 +31,12 @@ test('Test library functions', (t) => { t.equals(xl.getExcelTS(new Date('1900-01-01T12:00:00Z')), 1.5000000, 'Correctly translated date 1900-01-01T12:00:00Z'); t.equals(xl.getExcelTS(new Date('9999-12-31T23:59:59Z')), 2958465.9999884, 'Correctly translated date 9999-12-31T23:59:59Z'); + /** + * getExcelTS should handle dates within 2 days of daylight savings change (2020-03-08 for example) + */ + t.equals(xl.getExcelTS(new Date('2020-03-05T15:38:00Z')), 43895.6513889, 'Correctly translated date 2020-03-01T15:38:00Z'); + t.equals(xl.getExcelTS(new Date('2020-03-06T15:38:00Z')), 43896.6513889, 'Correctly translated date 2020-03-06T15:38:00Z'); + /** * Tests as defined in §18.17.4.1 of ECMA-376, Second Edition, Part 1 - Fundamentals And Markup Language Reference * The serial value 2.0000000... represents 1900-01-01