Skip to content

Fix some exceptional cases #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,23 @@ module.exports.getJsDateFromExcel = (excelDate) => {
const missingLeapYearDay = secondsInDay * 1000;
const delta = excelDate - (25567 + 2);
const parsed = delta * missingLeapYearDay;
const date = new Date(parsed)
const parsedDate = new Date(parsed);

const fractionalDay = excelDate - Math.floor(excelDate) + 0.0000001;
let totalSeconds = Math.floor(secondsInDay * fractionalDay);
const seconds = totalSeconds % 60;
totalSeconds -= seconds;
const hours = Math.floor(totalSeconds / (60 * 60));
const minutes = Math.floor(totalSeconds / 60) % 60;

const date = new Date(
parsedDate.getFullYear(),
parsedDate.getMonth(),
parsedDate.getDate(),
hours,
minutes,
seconds
);

if (Object.prototype.toString.call(date) === "[object Date]" ) {
if (isNaN(date.getTime())) {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"homepage": "https://github.com/oleg-koval/excel-date-to-js#readme",
"devDependencies": {
"mockdate": "^3.0.2",
"semantic-release": "^15.13.24",
"semantic-release-npm-github-publish": "^1.1.10",
"tape": "^4.8.0"
Expand Down
42 changes: 39 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,63 @@
const test = require('tape');
const mockDate = require('mockdate');

const { getJsDateFromExcel } = require('../index');

test('should convert Excel date to JS date', function (t) {
mockDate.set('2020-05-19T22:00:00.000Z');

const actual = getJsDateFromExcel(42510).toISOString();
const expected = new Date('2016-05-20T00:00:00.000Z').toISOString();
const expected = new Date('2016-05-19T22:00:00.000Z').toISOString();
t.equal(actual, expected);
t.end()

mockDate.reset();
});

test('should convert Excel date to JS date', function (t) {
mockDate.set('2020-05-19T22:00:00.000Z');

const actual = getJsDateFromExcel(43078.416666666664).toISOString();
const expected = new Date('2017-12-09T09:00:00.000Z').toISOString();
t.equal(actual, expected);
t.end()

mockDate.reset()
});

test('should convert Excel date to JS date (5/19/2013 21:29, from https://goo.gl/Ejbext)', function (t) {
mockDate.set('2020-05-19T22:00:00.000Z');

const actual = getJsDateFromExcel(41413.895150462966).toISOString();
const expected = new Date('2013-05-19T19:29:01.000Z').toISOString();
t.equal(actual, expected);
t.end()

mockDate.reset()
});

test('should convert return an error on wrong input', function (t) {
mockDate.set('2020-05-19T22:00:00.000Z');

try {
const actual = getJsDateFromExcel('text');
getJsDateFromExcel('text');
} catch ({ message }) {
t.equal(message, 'wrong input format')
}
t.end()

mockDate.reset()
});

test('should convert return an error on wrong excel date', function (t) {
mockDate.set('2020-05-19T22:00:00.000Z');

try {
const actual = getJsDateFromExcel(7312873827);
getJsDateFromExcel(7312873827);
} catch ({ message }) {
t.equal(message, 'wrong excel date input')
}
t.end()

mockDate.reset()
});