javascriptexceldatetimedatetime-format

Convert Javascript Date object to Excel serial date number


So I have a Javascript date object with year, month, days, hours, minutes, seconds, and milliseconds data and I need to convert this object to an Excel serial number but I didn't find a way to do that. I found how to convert only a Date object but the way I found didn't consider the time part.

Any suggestions? Thank you, Regards.


Solution

  • finally I was able to convert it properly, I used the following code to do so:

    let date = new Date();
    let converted = 25569.0 + ((date.getTime() - (date.getTimezoneOffset() * 60 * 1000)) / (1000 * 60 * 60 * 24));
    

    Where 25569 is the difference between Excel start date (1900/01/01) and Javascript start date (1970/01/01).

    Thank you all.