Convert a mySQL date to Javascript date

后端 未结 12 1792
没有蜡笔的小新
没有蜡笔的小新 2020-12-25 13:09

What would be the best way to convert a mysql date format date into a javascript Date object?

mySQL date format is \'YYYY-MM-DD\' (ISO Format).

12条回答
  •  离开以前
    2020-12-25 13:58

    Building onto Jhurisman's answer. If you would want to set the date's time as well use the following:

    var dateParts = mysqlDate;
    var timeParts = dateParts[2].substr(3).split(":");
    
    var jsDate = new Date(
                    dateParts[0],
                    dateParts[1] - 1,
                    dateParts[2].substr(0,2),
                    timeParts[0],
                    timeParts[1],
                    timeParts[2]
                );
    

提交回复
热议问题