在javascript中显示明天的名字?
我试图在我们的电子商务网站上输出类似于以下内容的内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种方式...
Another way...
这是一个也会跳过周末的单行代码:
如果 getDay 是星期五 (5),则 + 1 是 6,% 6 是 0,这是错误的,所以 || 1 使其成为 1(星期一)。
如果 getDay 是星期六 (6),则 + 1 为 7,% 6 为 1(星期一)
不需要维护一个并行数组。
Here's a one-liner that will also skip weekends:
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.