显示当天提前2天的当天名称

时间:2014-01-29 06:29:05

标签: javascript jquery html date

目前我正在做日历/调度程序。我如何显示当天提前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()];

3 个答案:

答案 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,因为getDay()会返回当天。

var n = weekday[d.getDay()+2];

以下是示例Fiddle

答案 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);
});

以下是演示:http://jsfiddle.net/wared/346L8/

基于ling.s's solutionhttp://jsfiddle.net/wared/346L8/2/