在javascript中显示明天的名字?

发布于 2024-10-15 03:31:04 字数 653 浏览 4 评论 0原文

我试图在我们的电子商务网站上输出类似于以下内容的内容:

Order by 5pm today for dispatch on Monday

显然,“星期一”一词将被第二天的名称替换(最好是下一个工作日,即不是星期六或星期日)。

我有以下简单的 javascript 脚本,它执行最基本的版本。它只是输出当前日期名称:

<p id="orderBy">
<script type="text/javascript"> 
  <!-- 
  // Array of day names
  var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
                "Thursday","Friday","Saturday");
  var now = new Date();
  document.write("Order by 5pm today for dispatch on " + dayNames[now.getDay()]);
  // -->
</script>
</p>

有没有办法将上面的代码操作为+1日期名称?所以它会输出明天的名字而不是今天的名字。另外,可以跳过周末吗?

I am trying to output something similar to the following on our ecommerce website:

Order by 5pm today for dispatch on Monday

Obviously, the word Monday would be replaced by the name of the next day (ideally the next working day i.e. not Saturday or Sunday).

I have the following simple javascript script that does the most basic version. It simply outputs the current day name:

<p id="orderBy">
<script type="text/javascript"> 
  <!-- 
  // Array of day names
  var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
                "Thursday","Friday","Saturday");
  var now = new Date();
  document.write("Order by 5pm today for dispatch on " + dayNames[now.getDay()]);
  // -->
</script>
</p>

Is there a way of manipulating the above code to +1 the day name? So it would output tomorrows name rather than today. Furthermore, is it possible to skip the weekends?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

优雅的叶子 2024-10-22 03:31:04

另一种方式...

<p id="orderBy">
<script type="text/javascript"> 
  <!-- 
  // Array of day names
  var dayNames = ["Sunday","Monday","Tuesday","Wednesday",
                "Thursday","Friday","Saturday"];
  var nextWorkingDay = [ 1, 2, 3, 4, 5, 1, 1 ];
  var now = new Date();
  document.write("Order by 5pm today for dispatch on " +
                 dayNames[nextWorkingDay[now.getDay()]]);
  // -->
</script>
</p>

Another way...

<p id="orderBy">
<script type="text/javascript"> 
  <!-- 
  // Array of day names
  var dayNames = ["Sunday","Monday","Tuesday","Wednesday",
                "Thursday","Friday","Saturday"];
  var nextWorkingDay = [ 1, 2, 3, 4, 5, 1, 1 ];
  var now = new Date();
  document.write("Order by 5pm today for dispatch on " +
                 dayNames[nextWorkingDay[now.getDay()]]);
  // -->
</script>
</p>
雨落□心尘 2024-10-22 03:31:04

这是一个也会跳过周末的单行代码:

document.write("Order by 5pm today for dispatch on " + 
                                  dayNames[ ((now.getDay() + 1) % 6 ) || 1 ] );
  • 如果 getDay 是星期五 (5),则 + 1 是 6,% 6 是 0,这是错误的,所以 || 1 使其成为 1(星期一)。

  • 如果 getDay 是星期六 (6),则 + 1 为 7,% 6 为 1(星期一)

  • < p>如果 getDay 是星期日(0),则 + 1 为 1,% 6 为 1(星期一)

不需要维护一个并行数组。

Here's a one-liner that will also skip weekends:

document.write("Order by 5pm today for dispatch on " + 
                                  dayNames[ ((now.getDay() + 1) % 6 ) || 1 ] );
  • If getDay is Friday (5), then + 1 is 6, % 6 is 0, which is falsey so || 1 makes it 1 (Monday).

  • If getDay is Saturday (6), then + 1 is 7, % 6 is 1 (Monday)

  • If getDay is Sunday (0), then + 1 is 1, % 6 is 1 (Monday)

No need to maintain a parallel Array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文