将UTC转换为mysql dateTime javascript

时间:2014-02-02 20:50:44

标签: javascript mysql

我无法转换示例字符串:'Sun Feb 02 19:12:44 +0000 2014'

到javascript中的mysql dateTime格式

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

试试这个:

var d = new Date('Sun Feb 02 19:12:44 +0000 2014');
var month = d.getMonth();
if(month < 10)
month = "0" + month;
var day = d.getDate();
if(day < 10)
day = "0" + day;
hour  = d.getHours();
if(hour < 10)
hour = "0" + hour;
minute = d.getMinutes();
if(minute < 10)
minute = "0" + minute;
seconds = d.getSeconds();
if(seconds < 10)
seconds = "0" + seconds;    
var mysqlDate = d.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + seconds; 

答案 1 :(得分:1)

您可以使用以下代码将日期时间转换为mysql格式:

var myDate = new Date('Sun Feb 02 19:12:44 +0000 2014'),
    year = myDate.getFullYear(),
    month = ('0' + (myDate.getMonth() + 1)).slice(-2),
    day = ('0' + myDate.getDate()).slice(-2),
    hour  = ('0' + myDate.getHours()).slice(-2),
    minute = ('0' + myDate.getMinutes()).slice(-2),
    seconds = ('0' + myDate.getSeconds()).slice(-2),
    mysqlDateTime = [year, month, day].join('-') + ' ' + [hour, minute, seconds].join(':');

但我建议将其作为时间戳(+(new Date('Sun Feb 02 19:12:44 +0000 2014')))或格式化字符串(myDate.toISOString())发送到后端,然后继续进行转换。