Skip to content

Instantly share code, notes, and snippets.

@christopherscott
Created May 24, 2012 16:40
Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save christopherscott/2782634 to your computer and use it in GitHub Desktop.
Save christopherscott/2782634 to your computer and use it in GitHub Desktop.
Convert Excel date values to JavaScript date objects
// Convert Excel dates into JS date objects
//
// @param excelDate {Number}
// @return {Date}
function getJsDateFromExcel(excelDate) {
// JavaScript dates can be constructed by passing milliseconds
// since the Unix epoch (January 1, 1970) example: new Date(12312512312);
// 1. Subtract number of days between Jan 1, 1900 and Jan 1, 1970, plus 1 (Google "excel leap year bug")
// 2. Convert to milliseconds.
return new Date((excelDate - (25567 + 1))*86400*1000);
}
@vijaykk17
Copy link

If I Number format the date column as 'Number' and want to receive only number after applying the functionit should not be converted to date. For example

Date

| 44000|

should be visible as 44000 in js also . If I could get the Number format of Excel column somehow in js

@rahul7007
Copy link

rahul7007 commented Sep 28, 2020

  1. install excel-date-to-js package (npm i excel-date-to-js)
  2. Run this snippet:
    const { getJsDateFromExcel } = require("excel-date-to-js")
    getJsDateFromExcel(dateFieldFromExcelFile)

@devexpert7
Copy link

You're a legend! Much appreciated!

By the way, I also had to adjust the value to 2 days such as: new Date((excelDate - (25567 + 2))_86400_1000)

What should be the excel field format? I tried with date format but get error as "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)".
Also, text format did not help. Number format is not readable to user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment