Formatting the date time with Javascript

后端 未结 12 2334
暗喜
暗喜 2020-11-27 07:36

I have a date/time string like 2012-01-13 04:37:20 but I want to convert it to dd-mm-yyyy hh:mm, how can i do this?

I am using the followin

12条回答
  •  执念已碎
    2020-11-27 07:58

    You can do a simple string manipulation and create js date object. See function below, which accepts date in format //yyyy-mm-dd hh:mm:ss

    DEMO here

    function toJSDate (dateTime) {
    
    var dateTime = dateTime.split(" ");//dateTime[0] = date, dateTime[1] = time
    
    var date = dateTime[0].split("-");
    var time = dateTime[1].split(":");
    
    //(year, month, day, hours, minutes, seconds, milliseconds)
    return new Date(date[0], date[1], date[2], time[0], time[1], time[2], 0);
    
    }
    

提交回复
热议问题