Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
29 changes: 29 additions & 0 deletions spec/unit/utils/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});