Skip to content

Instantly share code, notes, and snippets.

@mikebobadilla
Created January 10, 2016 04:29
Show Gist options
  • Save mikebobadilla/0af3672e54cbddb1ad5a to your computer and use it in GitHub Desktop.
Save mikebobadilla/0af3672e54cbddb1ad5a to your computer and use it in GitHub Desktop.
Excel serial number conversion to date in javascript
function ExcelDateToJSDate(serial) {
var utc_days = Math.floor(serial - 25569);
var utc_value = utc_days * 86400;
var date_info = new Date(utc_value * 1000);
var fractional_day = serial - Math.floor(serial) + 0.0000001;
var total_seconds = Math.floor(86400 * fractional_day);
var seconds = total_seconds % 60;
total_seconds -= seconds;
var hours = Math.floor(total_seconds / (60 * 60));
var minutes = Math.floor(total_seconds / 60) % 60;
return new Date(date_info.getFullYear(), date_info.getMonth(), date_info.getDate(), hours, minutes, seconds);
}
@lucasdu4rte
Copy link

Why this values returns equals datetimes?

Date in Excel: 2018-03-12 08:01:00
ExcelDateToJSDate(43171.33472)
return
"2018-03-12T08:01:00Z"

Date in Excel: 2018-03-12 08:02:00
ExcelDateToJSDate(43171.33403)
return
"2018-03-12T08:01:00Z"

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