Converting Excel Date Serial Number to Date using Javascript

0 votes

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?

Apr 11, 2022 in Database by Edureka
• 13,690 points
3,547 views

1 answer to this question.

0 votes

Please do 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 :)

answered Apr 11, 2022 by gaurav
• 23,260 points

Related Questions In Database

0 votes
1 answer

Convert a number to a letter in C# for use in Microsoft Excel [duplicate]

If you are familiar with using formulas ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
869 views
0 votes
1 answer

Converting HTML to Excel? [closed]

Upload html-file(s) Select files from Computer, Google ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
602 views
0 votes
1 answer

Converting a large XML file to Excel

Using the web URL, import an XML ...READ MORE

answered Mar 24, 2022 in Database by gaurav
• 23,260 points
2,872 views
0 votes
1 answer

Insert a value to a cell in excel using formula in another cell

Select the cell where the formula should ...READ MORE

answered Mar 25, 2022 in Database by gaurav
• 23,260 points
4,947 views
0 votes
1 answer

How to convert a column number (e.g. 127) into an Excel column (e.g. AA)

If anyone needs to do this in ...READ MORE

answered Mar 30, 2022 in Database by gaurav
• 23,260 points
1,047 views
0 votes
1 answer

Convert Date to Text without losing the format in Excel?

The steps are as follows: Copy the dates ...READ MORE

answered Apr 1, 2022 in Database by gaurav
• 23,260 points
40,009 views
0 votes
1 answer

Number Formatting to 10K 5M, etc in Excel 2016

In the format of a user-defined number [>=1000000]0,,"M";[>=1000]0,"K";0 The ...READ MORE

answered Apr 4, 2022 in Database by Edureka
• 13,690 points
1,739 views
0 votes
1 answer

How to download created excel file in node.js using exceljs

Download a sample Excel file using Node. ...READ MORE

answered Apr 4, 2022 in Database by Edureka
• 13,690 points
7,872 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP