gpt4 book ai didi

JavaScript\获取上周四的日期

转载 作者:行者123 更新时间:2023-11-28 11:02:22 26 4
gpt4 key购买 nike

如何通过 JavaScript 获取上周四的日期?

如果星期四是今天,则得到 -7 天。

之后的格式输出如“2016 年 4 月 17 日”。

最佳答案

首先,通过分享您的代码会更容易为您提供帮助;)

您可以使用方法 Date.getDay() 获取上周四,该方法返回一周中的当前日期。

像这样:

var now = new Date();
var daysAfterLastThursday = (-7 + 4) - now.getDay(); // 7 = number of days in week, 4 = the thursdayIndex (0= sunday)
var currentMs = now.getTime();
var lastThursday = new Date(currentMs + (daysAfterLastThursday * 24 * 60 * 60 * 1000));
alert("Last Thursday : " + lastThursday);

jsFiddle

关于JavaScript\获取上周四的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com