Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Excel Date Serial Number to Date using Javascript

I have the following javascript code that convert date (string) to the Date Serial Number used in Microsoft Excel:

function JSDateToExcelDate(inDate) {      var returnDateTime = 25569.0 + ((inDate.getTime() - (inDate.getTimezoneOffset() * 60 * 1000)) / (1000 * 60 * 60 * 24));     return returnDateTime.toString().substr(0,5);  } 

So, how do I do the reverse? (Meaning that a Javascript code that convert the Date Serial Number used in Microsoft Excel to a date string?

like image 252
Jack Avatar asked Apr 26 '13 05:04

Jack


People also ask

How do I change the date format in Excel?

Select the cells you want to format. Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.


2 Answers

Try this:

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); } 

Custom made for you :)

like image 52
silkfire Avatar answered Oct 08 '22 22:10

silkfire


I made a one-liner for you:

function ExcelDateToJSDate(date) {   return new Date(Math.round((date - 25569)*86400*1000)); } 
like image 28
Gil Avatar answered Oct 08 '22 23:10

Gil