Skip to content
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

feat: add ability to transform js date back to excel #20

Merged
merged 1 commit into from
Sep 23, 2021
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 index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// https://github.com/oleg-koval/excel-date-to-js/issues/16

declare module "excel-date-to-js" {
export function getJsDateFromExcel(excelDate: number): Date
export function getJsDateFromExcel(excelDate: string | number): Date
export function getExcelDateFromJs(date: Date): number
}
29 changes: 19 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
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);
Expand All @@ -14,18 +20,21 @@ module.exports.getJsDateFromExcel = (excelDate) => {
throw new Error('wrong input format')
}

const secondsInDay = 24 * 60 * 60;
const missingLeapYearDay = secondsInDay * 1000;
const delta = excelDate - (25567 + 2);
const parsed = delta * missingLeapYearDay;
const delta = excelDate - MAGIC_NUMBER_OF_DAYS;
const parsed = delta * MISSING_LEAP_YEAR_DAY;
const date = new Date(parsed)

if (Object.prototype.toString.call(date) === "[object Date]" ) {
if (isNaN(date.getTime())) {
if (!isDate(date)) {
throw new Error('wrong excel date input')
}
else {
return date
}
}

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
}
40 changes: 38 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
const test = require('tape');
const { getJsDateFromExcel } = require('../index');
const { getJsDateFromExcel, getExcelDateFromJs } = require('../index');

test('should convert Excel date to JS date', function (t) {
test('should convert Excel date to JS date (number)', function (t) {
const actual = getJsDateFromExcel(42510).toISOString();
const expected = new Date('2016-05-20T00:00:00.000Z').toISOString();
t.equal(actual, expected);
t.end()
});

test('should convert Excel date to JS date (string)', function (t) {
const actual = getJsDateFromExcel('42510').toISOString();
const expected = new Date('2016-05-20T00:00:00.000Z').toISOString();
t.equal(actual, expected);
t.end()
});

test('should convert return an error on wrong input', function (t) {
try {
const actual = getJsDateFromExcel('text');
Expand All @@ -25,3 +32,32 @@ test('should convert return an error on wrong excel date', function (t) {
}
t.end()
});

test('should convert JS date to Excel date', (t) => {
const date = new Date('2016-05-20T00:00:00.000Z');
const expected = 42510;

const actual = getExcelDateFromJs(date);

t.equal(actual, expected)

t.end()
});

test('should return an error on wrong input while converting JS date (wrong type)', function (t) {
try {
getExcelDateFromJs('text');
} catch ({ message }) {
t.equal(message, 'wrong input format')
t.end()
}
});

test('should return an error on wrong input while converting JS date (invalid date)', function (t) {
try {
getExcelDateFromJs(new Date('text'));
} catch ({ message }) {
t.equal(message, 'wrong input format')
t.end()
}
});