首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaScript -获取一周中下一天的日期(每一天)

JavaScript -获取一周中下一天的日期(每一天)
EN

Stack Overflow用户
提问于 2018-06-14 08:24:59
回答 2查看 128关注 0票数 0

我已经制作了一个在线日历,我想将日期添加到星期几的旁边。例如,它会显示为Monday (6/18)。我当前的代码运行得相当好,但是从星期一开始获取当前周的第几天。这意味着因为今天是星期三,所以它显示两天前的日期,而不是预期的下周一的日期。

当前代码:

代码语言:javascript
复制
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() + ")");

提前谢谢你!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-14 08:38:16

下面是一个示例:

从今天到接下来的6天。

代码语言:javascript
复制
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() + ")");
代码语言:javascript
复制
<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获取给定日期的下一周日期的另一个示例:

代码语言:javascript
复制
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() + ")");
代码语言:javascript
复制
<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/>

票数 1
EN

Stack Overflow用户

发布于 2018-06-14 08:33:32

如果你想看看下周的数值,为什么不多加7呢?

代码语言:javascript
复制
var mon = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+8);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50848012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档