Converting Excel Date Serial Number to Date using Javascript

后端 未结 9 1356
终归单人心
终归单人心 2020-11-29 02:22

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

function JSDateToExcelDate(inDate) {

             


        
9条回答
  •  鱼传尺愫
    2020-11-29 03:07

    So, there I was, having the same problem, then some solutions bumped up but started to have troubles with the Locale, Time Zones, etc, but in the end was able to add the precision needed

    toDate(serialDate, time = false) {
        let locale = navigator.language;
        let offset = new Date(0).getTimezoneOffset();
        let date = new Date(0, 0, serialDate, 0, -offset, 0);
        if (time) {
            return serialDate.toLocaleTimeString(locale)
        }
        return serialDate.toLocaleDateString(locale)
    }
    

    The function's 'time' argument chooses between displaying the entire date or just the date's time

提交回复
热议问题