0

I have the following script:

document.write('<p><span id="date-time">', new Date().toLocaleString(), '<\/span>.<\/p>')
if (document.getElementById) onload = function () {
    setInterval("document.getElementById ('date-time').firstChild.data = new Date().toLocaleString()", 50)
}

It displays:

Friday, January 09, 2015 12:20 PM.

How can I display tomorrow's date in the same format leaving the time off?

lante
  • 7,192
  • 4
  • 37
  • 57
almen4
  • 5
  • 3
  • 1
    [JavaScript how to get tomorrows date](http://stackoverflow.com/questions/9444745/javascript-how-to-get-tomorrows-date-in-format-dd-mm-yy) – adeneo Jan 09 '15 at 20:24
  • Watch out, your currently accepted answer won't work across all locales! – Qwerty Jan 09 '15 at 20:58

4 Answers4

2

You can do it setting custom options for toLocaleString, see MDN documentation for toLocaleString

To solve your question:

var tomorrow = new Date(Date.now() + 1000 * 3600 * 24);
var result = tomorrow.toLocaleString('en-US', { weekday: 'long', month: 'long', year: 'numeric', day: 'numeric' });

document.write('<p><span id="date-time">', result, '<\/span>.<\/p>');

See this fiddle.

lante
  • 7,192
  • 4
  • 37
  • 57
1

This will add +1 to result of getDate(), which is current day, then prints with given formatting.

var tomorrow = new Date(new Date().setDate(new Date().getDate()+1));
console.log(tomorrow.toLocaleString('en-US', { weekday: 'long', month: 'long', year: 'numeric', day: 'numeric' }));

Note that this will print the date in same format across all destinations and languages, unlike the toLocaleString() without parameters.

...

/edit
Possibly neater way to increment the day by one is

var date = new Date();
date.setDate(date.getDate() + 1);
// date.toLocaleString(...) remains the same as above
Qwerty
  • 29,062
  • 22
  • 108
  • 136
0

A date can be represented by the number of milliseconds since the epoch (1 January, 1970, 00:00:00 UTC in javascripts case)

One day is 86400000 milliseconds (24 * 60 * 60 *1000)

Create a Date object for today,then get the millisecond representation (getTime) and add one days worth of milliseconds

Create (using the constructor) or modify a Date object (using setTime method) from this millisecond representation and then use the toDateString method to return just the date portion as a string.

Or for a language sensitive representation use toLocaleDateString

Note: toLocaleDateString has limited browser support and so you may have to perform manual formatting yourself.

Where can I find documentation on formatting a date in JavaScript?

var todayObj = new Date()
    tomorrowMs = todayObj.getTime() + 86400000,
    tomorrowObj = new Date(tomorrowMs),
    tomorrowDateStr = tomorrowObj.toDateString();

document.body.appendChild(document.createTextNode(tomorrowDateStr));
Community
  • 1
  • 1
Xotic750
  • 22,914
  • 8
  • 57
  • 79
-3
var tomorrow = new Date();
 tomorrow.setDate(tomorrow.getDate() + 1);
 var str = tomorrow.toLocaleString().substring(0,tomorrow.toLocaleString().indexOf(':')-3);

document.write('<p><span id="date-time">', str, '<\/span>.<\/p>')
if (document.getElementById) onload = function () {
setInterval("document.getElementById ('date-time').firstChild.data = str", 50)
}

As @Qwerty noted, you will not always get the exact same format in all computers.

Tomer
  • 3,149
  • 23
  • 26
  • that one gave me the result I wanted, just had to change -4 to a -3. Thank you, very much appreciated. – almen4 Jan 09 '15 at 20:45
  • Okay, I changed it to -3 in my answer. – Tomer Jan 09 '15 at 20:49
  • This is not a general solution as it will look different with different locale setting! This is what I see as a result `10. 1. 2015`. – Qwerty Jan 09 '15 at 20:53
  • The asker displayed today's date without locale stuff. – Tomer Jan 09 '15 at 20:54
  • The asker wanted to see `Saturday, January 10, 2015`, but what I get from your `str` variable is `10. 1. 2015`. – Qwerty Jan 09 '15 at 20:55
  • He wanted to see it on his computer. this is what he saw. nobody said he wanted to see this exact format in all computers. It's also the example he provided. – Tomer Jan 09 '15 at 20:57
  • @Tomer Because he isn't obviously aware of the fact, that the Date prints differently across all computers. You have the experience, yet you didn't say anything! – Qwerty Jan 09 '15 at 21:04
  • This is just a hack, not a solution. Shame on you. – Qwerty Jan 09 '15 at 21:04
  • @Qwerty I don't think it is a hack. you will always get tomorrow's date without the time, which is what the asker asked for. Anyway, I will note that in my answer. – Tomer Jan 09 '15 at 21:08
  • The hacky thing is to use `.indexOf()` to cut certain parts of the resulting string, instead of using native formatting abilities. Especially when `toLocaleString()` formats differently across all computers. – Qwerty Jan 09 '15 at 21:20
  • @Qwerty Is there any locale setting where it won't show tommorow's date? – Tomer Jan 09 '15 at 21:23
  • 1
    Yes, on any browser that doesn't support the method or optional arguments. :) – Xotic750 Jan 09 '15 at 21:26
  • @Tomer Hahah! The real question is _"Is there any locale setting where it won't show the date in the same format, leaving the time off?"_ as the asker asked. – Qwerty Jan 09 '15 at 21:26
  • what method? `toLocaleString()`? And I don't think the exact format matters so much. He was supposed to know that if he uses toLocaleString() it will depend on the locale setting. – Tomer Jan 09 '15 at 21:27
  • @Tomer Yeah, well, people are different and even though something says "don't press this button, it will hurt you" they will hurt themselves. You have just let this guy burn himself greately, when he discovers what has he done a year later. But then that's the best lesson, I guess. – Qwerty Jan 09 '15 at 21:29
  • @Tomer Chill, the OPs question was not specific and appears to lack any research. ;) – Xotic750 Jan 09 '15 at 21:30
  • Wait, I just want to understand something. Can this code show something that it is simply WRONG, or at worst in a different format? If it can't show anything wrong, I think the note I added is enough. – Tomer Jan 09 '15 at 21:32
  • 1
    It could be that certain locales do not use `:` for its time separation. – Xotic750 Jan 09 '15 at 21:38
  • Yeah, this is what I was thinking, but I can't think of any other way to separate time. – Tomer Jan 09 '15 at 21:39
  • 1
    [`toLocaleDateString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#Parameters) already has the time part removed. – Xotic750 Jan 09 '15 at 21:40
  • @Xotic750 But I don't think there is a locale setting where `:` is not used to separate time. – Tomer Jan 09 '15 at 21:48
  • That I can neither confirm nor deny, there are a huge number of locales, and I am not aware of them all. You could research it on [`CLDR`](http://cldr.unicode.org/) The basic time format certainly uses it [date-time-patterns](http://cldr.unicode.org/translation/date-time-patterns) – Xotic750 Jan 09 '15 at 21:57