-1

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.

AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
Sami
  • 1
  • 1
  • 5
  • you just need to format the date object, the way you want to see. Possible duplicate for http://stackoverflow.com/questions/5250244/jquery-date-formatting – Anil Pediredla Oct 20 '16 at 18:56

3 Answers3

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>
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • `var str = ... ` line is unused here. Also, this does not offset for Sat/Sun to both return "Monday". This can be accomplished with a switch statement – mhodges Oct 20 '16 at 19:02
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

gamboa
  • 141
  • 3
  • This does not offset for Sat/Sun to both return "Monday". This can be accomplished with a switch statement – mhodges Oct 20 '16 at 19:02
  • thanks for the help. If its Saturday or Sunday it should say appointments are available from Monday. – Sami Oct 20 '16 at 19:59
  • Sorry, didnt see that edit. I already updated the answer to achieve this. (Just changed the weekday[0] and weekday[6] values to "Monday") – gamboa Oct 20 '16 at 20:08
0

You're correct that const date = new Date(); date.setDate(date.getDate() + 1); is how you get a date representing 1 day in the future. JavaScript's toLocaleString() takes an options parameter that lets you get just the day of the week in the given locale, so you don't need the extra indexOf() or hardcoded weekday names.

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var str = tomorrow.toLocaleString('en-US', { weekday: 'long' }); // "Friday"

Read more here: https://masteringjs.io/tutorials/fundamentals/tomorrow

vkarpov15
  • 3,614
  • 24
  • 21