如何通过提供日期格式'yyyy-mm-dd'来获取当天的名字?
根据correct format,我的'2015-03-03'
日期为new Date(dateString);
{{1}}构造函数。
如何获取星期二的日期名称?
答案 0 :(得分:1)
我找到了这个问题的解决方案,所以我想与大家分享一下,使用日期格式获取当天的名字' yyyy-mm-dd'你必须使用javascript文件moment.js并使用此函数获取所有一天的信息:
var dayInformation = String(moment('2015-03-10'));
答案 1 :(得分:1)
由于时区偏离设置,您可能在一周中遇到错误的一天。您可以通过将本地时区偏移(以分钟为单位)乘以一分钟内的毫秒数来纠正此问题。
var getDayOfWeek = (function () {
var days = ['Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'];
return function (date) {
return days[date.getDay()];
};
}());
function getLocalizedDate(dateStr) {
var d = new Date(dateStr);
return new Date(d.getTime() + d.getTimezoneOffset() * 60000);
}
function daysInMarch() {
var display = document.querySelector('#display'),
i = 1,
dateStr, date, formatted;
for (; i <= 31; i++) {
dateStr = '2015-03-' + (i < 10 ? '0' + i : i),
date = getLocalizedDate(dateStr),
formatted = 'March ' + date.getDate() + ' 2015 was a ';
display.innerHTML += formatted + getDayOfWeek(date) + '<br />';
}
}
daysInMarch();
&#13;
<div id="display"></div>
&#13;
答案 2 :(得分:1)
我已经得到了昨天写的这个片段。可能对你有用。
/**
* Created by jjansen on 09-Mar-15 4:56 PM.
* stackoverflow
*/
var dateName = function ( yyyy, mm, dd ) {
var monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
var dayNames = [
"Monday",
"Thuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
];
var today = new Date( yyyy, mm-1, dd );
var dd = today.getDate();
var dayname = dayNames[today.getDay()-1];
var mm = monthNames[today.getMonth()];
var yyyy = today.getFullYear();
var fullDate = dayname + " " + dd + " " + mm + " " + yyyy;
alert( "the current date is: " + fullDate );
};
dateName( 2015, 06, 02 );
你可以任何你想要的方式改变它,它完美无瑕
<小时/> http://jsfiddle.net/vuob3L16/