我在json中有以下日期时间值:
Fri Jan 22 2016 14:34:38 GMT-0500
我想显示"January 22, 2016"
我怎样才能在javascript中实现这一点。我有JQuery,Extjs库。
答案 0 :(得分:1)
尝试使用for..in
循环,String.prototype.slice()
,String.prototype.replace()
var months = {
"Jan":"January",
"Feb":"February",
"Mar":"March",
"Apr":"April",
"May":"May",
"Jun":"June",
"Jul":"July",
"Aug":"August",
"Sep":"September",
"Oct":"October",
"Nov":"November",
"Dec":"December"
};
var date = "Fri Jan 22 2016 14:34:38 GMT-0500";
// extract "Jan 22 2016" from `date`
var d = date.slice(4, -18);
for (var prop in months) {
if (new RegExp(prop).test(d)) {
// replace abbreviated month with full month name
d = d.replace(prop, months[prop]);
// replace day with day followed by comma `,` character
d = d.replace(/(\d{2})(?=\s)/, "$1,")
}
}
document.body.textContent = d
答案 1 :(得分:0)
这个问题针对的是你的同一个问题。您可以使用此处显示的函数来构建日期字符串。
// This could be any Date String
var str = "Fri Feb 08 2013 09:47:57 GMT +0530 (IST)";
var date = new Date(str);
这将使您可以访问所有日期函数(MDN)
例如:
var day = date.getDate(); //Date of the month: 2 in our example
var month = date.getMonth(); //Month of the Year: 0-based index, so 1 in our example
var year = date.getFullYear() //Year: 2013
答案 2 :(得分:0)
在这里找到了这个疯狂的方法,但是有效!
Converting milliseconds to a date (jQuery/JS)
这是我做过的小提琴
https://jsfiddle.net/Ripper1992/hj6L2Lvz/
var now = new Date("Fri Jan 22 2016 14:34:38 GMT-0500");
alert(now.customFormat( "#MMMM# #DD#, #YYYY#" ) );
customFormat 是调用的函数,用于获取数据的每个部分,根据 #MMMM#或 #DD#进行解析和替换或 #SS#由用户定义。
以下是文档的完整功能 http://phrogz.net/JS/FormatDateTime_JS.txt