JavaScript - 获取本周第二天的日期(每天)?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (112)

我制作了一个在线日历,我想在一周中的某天添加日期。例如,它会说Monday (6/18)。我目前的代码运行得很好,但从周一开始获取本周的一天。这意味着从今天开始是星期三,它显示两天前的日期,而不是下个星期一,这是预期的效果。

当前代码:

var today = new Date();
var mon = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+1);
var tue = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+2);
var wed = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+3);
var thu = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+4);
var fri = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+5);
var sat = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+6);
var sun = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+7);

$(".mon").html("Monday (" + (mon.getMonth() + 1) + "/" + mon.getDate() + ")");
$(".tue").html("Tuesday (" + (tue.getMonth() + 1) + "/" + tue.getDate() + ")");
$(".wed").html("Wednesday (" + (wed.getMonth() + 1) + "/" + wed.getDate() + ")");
$(".thu").html("Thursday (" + (thu.getMonth() + 1) + "/" + thu.getDate() + ")");
$(".fri").html("Friday (" + (fri.getMonth() + 1) + "/" + fri.getDate() + ")");
$(".sat").html("Saturday (" + (sat.getMonth() + 1) + "/" + sat.getDate() + ")");
$(".sun").html("Sunday (" + (sun.getMonth() + 1) + "/" + sun.getDate() + ")");
提问于
用户回答回答于

这里是一个例子:

var arr = [];
var today = new Date();
for(var i=0;i<7;i++){
var date = new Date(today.getFullYear(), today.getMonth(), today.getDate()+i);
  arr[date.getDay()] = date;
}

$(".mon").html("Monday (" + (arr[1].getMonth() + 1) + "/" + arr[1].getDate() + ")");
$(".tue").html("Tuesday (" + (arr[2].getMonth() + 1) + "/" + arr[2].getDate() + ")");
$(".wed").html("Wednesday (" + (arr[3].getMonth() + 1) + "/" + arr[3].getDate() + ")");
$(".thu").html("Thursday (" + (arr[4].getMonth() + 1) + "/" + arr[4].getDate() + ")");
$(".fri").html("Friday (" + (arr[5].getMonth() + 1) + "/" + arr[5].getDate() + ")");
$(".sat").html("Saturday (" + (arr[6].getMonth() + 1) + "/" + arr[6].getDate() + ")");
$(".sun").html("Sunday (" + (arr[0].getMonth() + 1) + "/" + arr[0].getDate() + ")"); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="mon"></span><br/>
<span class="tue"></span><br/>
<span class="wed"></span><br/>
<span class="thu"></span><br/>
<span class="fri"></span><br/>
<span class="sat"></span><br/>
<span class="sun"></span><br/>

下面是使用getNextDay函数获取给定日期的第二天的另一个示例:

var arr = [];
var today = new Date();
for(var i=0;i<7;i++){
  arr[i] = getNextDay(today, i);
}

function getNextDay(date, day){
  var difference = day - date.getDay();
  if(difference < 0)
    difference += 7;
  return new Date(date.getFullYear(), date.getMonth(), date.getDate() + difference);
}

$(".mon").html("Monday (" + (arr[1].getMonth() + 1) + "/" + arr[1].getDate() + ")");
$(".tue").html("Tuesday (" + (arr[2].getMonth() + 1) + "/" + arr[2].getDate() + ")");
$(".wed").html("Wednesday (" + (arr[3].getMonth() + 1) + "/" + arr[3].getDate() + ")");
$(".thu").html("Thursday (" + (arr[4].getMonth() + 1) + "/" + arr[4].getDate() + ")");
$(".fri").html("Friday (" + (arr[5].getMonth() + 1) + "/" + arr[5].getDate() + ")");
$(".sat").html("Saturday (" + (arr[6].getMonth() + 1) + "/" + arr[6].getDate() + ")");
$(".sun").html("Sunday (" + (arr[0].getMonth() + 1) + "/" + arr[0].getDate() + ")");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="mon"></span><br/>
<span class="tue"></span><br/>
<span class="wed"></span><br/>
<span class="thu"></span><br/>
<span class="fri"></span><br/>
<span class="sat"></span><br/>
<span class="sun"></span><br/>
用户回答回答于

如果你想看下周的价值为什么不增加所有额外的7,即

var mon = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+8);

扫码关注云+社区

领取腾讯云代金券