我有下面的字符串格式数据
Dec 26, 2014, 09:56 ET
Dec 31, 2014, 21:30 ET
Dec 30, 2014, 13:36 ET
我想要以下面的日期格式输出
26-12-2014 09:56:00
31-12-2014 21:30:00
30-12-2014 13:36:00
我尝试使用下面的代码,但它给出了不同的值。纠正我可以应用哪种语法将字符串转换为日期
str2date(input,"MMM dd, YYYY, HH:mm 'ET'")
答案 0 :(得分:1)
如果您不想使用外部库,可以试试这个
<html>
<head>
<script type="text/javascript">
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function myDate ()
{
var dateValue = new Date("Dec 26, 2014, 09:56");
var d = dateValue.getDate();
var m = dateValue.getMonth();
var y = dateValue.getFullYear();
var h = addZero(dateValue.getHours());
var mi = addZero(dateValue.getMinutes());
var s = addZero(dateValue.getSeconds());
var newDate = (d + '-' + m + '-' + y + ' ' + h + ':' + mi + ':' +s);
document.write(newDate);
}
</script>
</head>
<body>
<script>
myDate();
</script>
</body>
</html>
答案 1 :(得分:0)
使用图书馆是最好的选择。以下是使用MomentJS;
的示例
var input = [
'Dec 26, 2014, 09:56 ET',
'Dec 31, 2014, 21:30 ET',
'Dec 30, 2014, 13:36 ET'
];
var output = input.map(function(dateString) {
return moment(dateString, "MMM DD, YYYY, HH:mm ZZ").format('DD-MM-YYYY HH:mm:ss');
});
console.log(JSON.stringify(output, undefined, ' '));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
&#13;
输出:
[
"26-12-2014 04:56:00",
"31-12-2014 16:30:00",
"30-12-2014 08:36:00"
]
答案 2 :(得分:0)
您好您可以使用Moment.js
轻松实现此目的
<html>
<head>
<script src="http://momentjs.com/downloads/moment.min.js"></script>
</head>
<body>
<script>
var inputDate = 'Dec 26, 2014, 09:56:00';
var formatString = 'DD-MM-YYYY hh:mm:ss';
var outputDate = moment(inputDate).format(formatString);
document.write(outputDate);
</script>
</body>
</html>
&#13;
答案 3 :(得分:0)
<script>
function strDate(str){
var m = str.match(/(\w+) (\d{1,2}), (\d{4}), (\d{1,2}):(\d{1,2})/);
var month = [];
month['Jan'] = 1;
month['Feb'] = 2;
month['Mar'] = 3;
month['Apr'] = 4;
month['May'] = 5;
month['Jun'] = 6;
month['Jul'] = 7;
month['Aug'] = 8;
month['Sep'] = 9;
month['Oct'] = 10;
month['Nov'] = 11;
month['Dec'] = 12;
return (m[2] + '-' + month[m[1]] + '-' + m[3] + ' ' + m[4] + ':' + m[5] + ':00');
}
alert(strDate("Dec 26, 2014, 09:56 ET"));
</script>