我正在尝试将下面的数据格式更改为另一种形式。
这是我收到的日期
var datetime =Thu Oct 03 2013 13:41:06 GMT+0530 (IST)
如何获取这种格式的日期
03-10-2013 13:41 PM
最佳答案
为什么不编写一个简短的函数来分割日期字符串。然后您可以按照您喜欢的顺序将它们重新粘在一起。
这是一个简单的示例:
function tidyDate(theDate) {
// split up the string into parts separated by colons or whitespace
var parts = theDate.toString().split(/[:\s]+/);
// Get the number of the month - Don't forget that it's zero-indexed
var month = theDate.getMonth() + 1;
// Let's say that it's morning
var AMPM = " AM";
// But we should check whether it's after noon
if (parseInt(parts[4]) >= 12){
AMPM = " PM";
}
return parts[2] + "-" + month + "-" + parts[3] + " " + parts[4] + ":" + parts[5] + AMPM;
}
tidyDate(new Date());
返回:
03-10-2013 13:41 PM
关于javascript - 使用 JavaScript 获取特定日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19156683/