Im trying to show the date of tomorrow in html with javascript. If today is "Monday" it show say "Tuesday" instead of showing the actual date and for weekends it should say "Monday"
<p>Appointments are available from next day. We can come out to you from <span id="nextDay"></span> onwards.</p>
<script>
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var str = tomorrow.toLocaleString().substring(0,tomorrow.toLocaleString().indexOf(':')-3);
document.getElementById("nextDay").innerHTML = tomorrow.toDateString();
</script>
that code shows something like this: Appointments are available from next day. We can come out to you from Fri Oct 21 2016 onwards.
but it show just say "Friday" instead of "Fri Oct 21 2016" and the code needs adjustment to show Monday if its Saturday or Sunday.
答案 0 :(得分:0)
your question is here: http://www.w3schools.com/jsref/jsref_getday.asp
But if you want an easy to use library to help with doing dates: http://momentjs.com/ (even has all the locales support in there, if you need it)
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var weekday = new Array(7);
weekday[0]= "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[tomorrow.getDay()];
document.getElementById("nextDay").innerHTML = n;
<div id="nextDay"></div>
答案 1 :(得分:0)
Based on this
var tomorrow = new Date();
var weekday = new Array(7);
weekday[0]= "Monday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Monday";
tomorrow.setDate(tomorrow.getDate() + 1);
document.getElementById("nextDay").innerHTML = weekday[tomorrow.getDay()];
<p>Appointments are available from next day. We can come out to you from <span id="nextDay"></span> onwards.</p>
this is pure javascript, not jQuery specific
答案 2 :(得分:0)
您说得对,const date = new Date(); date.setDate(date.getDate() + 1);
是您获取代表未来 1 天的日期的方式。 JavaScript 的 toLocaleString()
接受一个 options
参数,它可以让您仅获取给定语言环境中的星期几,因此您不需要额外的 indexOf()
或硬编码的工作日名称。
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var str = tomorrow.toLocaleString('en-US', { weekday: 'long' }); // "Friday"
在此处阅读更多信息:https://masteringjs.io/tutorials/fundamentals/tomorrow