目前我正在做日历/调度程序。我如何显示当天提前2天的日期名称。(例如今天是星期三,所以我假设要显示星期五)目前我所做的就是这个。
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var n = weekday[d.getDay()];
答案 0 :(得分:2)
演示FIDDLE
<强> Jquery的强>
var d=new Date();
d.setDate(d.getDate()+2);
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
alert(weekday[d.getDay()]);
答案 1 :(得分:1)
答案 2 :(得分:1)
在以下示例中,+d
提供与d.getTime()
相同的结果:
// 3600000ms (=1h) x 48 = 2 days
var n = weekday[new Date(+d + 3600000 * 48).getDay()]
我也非常喜欢ling.s's approach,但它需要一些修复:
// friday.getDay() -> 5
// weekday[5 + 2] -> undefined
// weekday[(5 + 2) % 7] -> "Sunday"
var n = weekday[(d.getDay() + 2) % 7];
以下是展示它的一种方法:
<span id="twoDaysLater"></span>
var weekday = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
var now = new Date();
var twoDaysLater = new Date(+now + 3600000 * 48);
var dayName = weekday[twoDaysLater.getDay()];
jQuery(function ($) {
// DOM is now ready
$('#twoDaysLater').text(dayName);
});