gpt4 book ai didi

JavaScript 显示日期

转载 作者:行者123 更新时间:2023-11-28 01:41:01 25 4
gpt4 key购买 nike

我想显示昨天的日期和日期。

今天日期的脚本:

<script type="text/javascript">
<!--
// Array of day names
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
// Array of month Names
var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var now = new Date();
document.write(dayNames[now.getDay()] + ", " +
monthNames[now.getMonth()] + " " +
now.getDate() + ", " + now.getFullYear());
// -->
</script>

我知道添加 -1 会更改日期/年份,但它不会成功更改星期/月份的日期,因为我们已在数组中列出了日期/月份,因此星期日和之前没有任何内容一月之前没有任何事情。

如何显示昨天/几天前的日期,确保日期和月份会发生变化?

最佳答案

你可以尝试这个:参见 DEMO

dayNames[now.getDay()==0?6:now.getDay()-1]

编辑:

将 setDate 设置为昨天: Demo

now.setDate(now.getDate()-1); // set yesterday for now

关于JavaScript 显示日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927257/

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