我使用youtube API提取日期并将其注入div。我正在尝试正确格式化日期,只显示没有时间方面的日期
1获取日期和格式字符串
var date = new Date(data.entry.published.$t.substr(0, 4), data.entry.published.$t.substr(5, 2) - 1, data.entry.published.$t.substr(8, 2)).toLocaleDateString();
2格式化日期字符串
var objDate = new Date(date), locale = "en-us", month = objDate.toLocaleString(locale, { month: "long" });
3显示
$('h5').html(objDate);
然而,这将于2013年10月7日星期六00:00:00 GMT-0500(东部标准时间)执行。
我的问题是如何将字符串格式化为仅显示月份日和年份
答案 0 :(得分:0)
您是否尝试过getMonth及其相关功能,如下所示:
var myDate = new Date("February 3 2001");
document.write((myDate.getMonth() + 1) + "-" + myDate.getDate() + "-" + myDate.getFullYear())
答案 1 :(得分:0)
您需要将日期对象格式化为字符串,以使其显示您希望的方式。
formatDate = function(dateObj) {
var months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
],
days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
d = new Date(dateObj),
day = days[d.getDay()],
date = d.getDate(),
month = months[d.getMonth()],
year = d.getFullYear();
return day + " " + month + " " + date + ", " + year;
};
var newDate = new Date();
alert( formatDate( newDate ) );
这里的工作示例:http://jsfiddle.net/QCY54/