JavaScript - 获取一周中下一天的日期(对于每一天)
Posted
技术标签:
【中文标题】JavaScript - 获取一周中下一天的日期(对于每一天)【英文标题】:JavaScript - Get the Date of the next Day of the Week (For each day) 【发布时间】:2018-06-14 00:24:59 【问题描述】:我制作了一个在线日历,我想在星期几旁边添加日期。例如,它会说Monday (6/18)
。我当前的代码运行良好,但从星期一开始获取当前星期的日期。这意味着由于今天是星期三,它显示两天前的日期,而不是 next 星期一,这是预期的效果。
当前代码:
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() + ")");
提前谢谢你!
【问题讨论】:
【参考方案1】:这是一个例子:
从今天到接下来的 6 天。
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/>
【讨论】:
【参考方案2】:如果您想查看下周的值,为什么不将 all 增加额外的 7,即
var mon = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay()+8);
【讨论】:
这样,明天的日期将是 8 天而不是 1 天。如果这一天过去了,我只想要下周。以上是关于JavaScript - 获取一周中下一天的日期(对于每一天)的主要内容,如果未能解决你的问题,请参考以下文章