如何将日期时间字符串转换为指定的日期时间格式

how to convert a date time string unto a specified date time format

任何人都知道如何转换这个

Wed Dec 02 2015 00:00:00 GMT+0800 (China Standard Time)

2015-12-02 00:00:00

任何想法、帮助、线索、建议、推荐都将不胜感激。

试试这个:

var temp = new Date("Wed Dec 02 2015 00:00:00 GMT+0800 (China Standard Time)");
console.log(temp.getFullYear() + "-"  +(temp.getMonth() + 1) + "-" + temp.getDate());
console.log(temp.getHours() + ":" + temp.getMinutes() + ":" + temp.getSeconds());
var d = Date.parse("Wed Dec 02 2015 00:00:00 GMT+0800");
d = new Date(d).toLocaleString();

然后使用这个:

var d = Date.parse("Wed Dec 02 2015 00:00:00 GMT+0800");
d = new Date(d).toLocaleString();
d.replace(/\d+\/\d+\/\d{4},/, function (x){
   x = x.replace(/,/, "");
   x = x.split("/");
   return x[2] + "-" + x[1] + "-" + x[0]; 
});

对不起,只好运行下楼了。忘掉正则表达式,把它切开并像这样一起使用。 (http://jsfiddle.net/u2xn2Lv5/1/)

JS:

var origTime = "Wed Dec 02 2015 00:00:00 GMT+0800 (China Standard Time)";
//or function you call to derive this data stringified

var dayVal = origTime.slice(8,10);
var yearVal = origTime.slice(11,15);
var monthVal = origTime.slice(4,7);
var timeVal = origTime.slice(16,23);
var newDate = ""; // Hasn't been set yet
//alert(monthVal);
switch(monthVal) {
    case "Dec":
        newDate = "12";
        break;
    case "Jan":
        newDate = "1";
        break;
    case "Jun":
        newDate = "6";
        break;
    default:
        "unknown month!"
};

var newTime =  newDate + "-" + dayVal + "-" + yearVal + "-" + timeVal;
document.getElementById("spanthing").innerHTML = newTime;

HTML:

<div id="spanthing"></div>

根据需要重新排列!干杯!

直接选择 moment.js

var time = 'Wed Dec 02 2015 00:00:00 GMT+0800 (China Standard Time)';
var format = moment(time).format('YYYY-MM-DD HH:mm:ss');

这里是jsfiddle

你可以试试这个

moment(yourDate).format("YYYY-MM-DD HH:mm:ss");