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

时间:2015-12-07 07:18:53

标签: javascript jquery datetime momentjs

任何人都知道如何转换

ssh-keygen -t rsa  -C $1

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

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

5 个答案:

答案 0 :(得分:1)

试试这个:

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());

答案 1 :(得分:1)

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]; 
});

答案 2 :(得分:1)

抱歉,不得不跑到楼下。忘记正则表达式,只需将其切片并像这样一起使用它。 (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>

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

答案 3 :(得分:1)

选择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

答案 4 :(得分:1)

你可以试试这个

int