I want to make a Javascript date object eg. var now = new Date().getTime()
that is set for "tomorrow at 8am", how would I accomplish that?

- 9,333
- 30
- 83
- 143
-
2Make a Date instance, add 1 to the day-of-month, then set the hours, minutes, seconds, and milliseconds. [Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) – Pointy Mar 22 '16 at 15:13
-
How do I add 1 to day of the month of my current date? – Barry Michael Doyle Mar 22 '16 at 15:17
-
There are many questions on SO for that. See http://stackoverflow.com/questions/9444745/javascript-how-to-get-tomorrows-date-in-format-dd-mm-yy and http://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date – Tushar Mar 22 '16 at 15:18
2 Answers
You could do the following:
var now = new Date();
now.setDate(now.getDate() + 1)
now.setHours(8);
now.setMinutes(0);
now.setMilliseconds(0);
also check this
You could also: var now = Date("2016-03-23T8:00:00");
And var now = new Date(2016,03,23,8,0,0,0 );
-
1If I'm not mistaken when using your first block of code, the minutes and seconds will be the current ones (i.e. if the alarm is set at 17:35, it will ring the next day at 8:35) – Aaron Mar 22 '16 at 15:29
-
Hmm, according to the MDN, you can [set the milliseconds with `setSeconds`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date/setSeconds), but [not the other way around](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date/setMilliseconds) – Aaron Mar 22 '16 at 15:35
-
-
I took my information from [w3schools](http://www.w3schools.com/jsref/jsref_obj_date.asp). The above seems valid. – Rana Mar 22 '16 at 15:38
-
2w3schools has a bad reputation here. While I believe most their content is correct, and definitely user-friendly, I've heard many times that they published erroneous content and took ages to respond to criticism and remove the errors. Let's get back to the source then : the ECMA specification. Here is [Date.prototype.setHours](http://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.sethours). We can see two interesting things : first, it returns a `TimeClip`, which is defined above as an integer. So there's no chaining possible, as you would be calling `
.setMinutes`. – Aaron Mar 22 '16 at 15:46 -
However, more interestingly, we can also see that it accepts much more arguments than just the hour : you can provide it with minutes, seconds, milliseconds, removing the need for chaining ! – Aaron Mar 22 '16 at 15:47
-
(note that I only checked the linked ECMA-6 spec, which older browsers do not implement. If compability is an issue, OP should check the ECMA 5.1 spec) – Aaron Mar 22 '16 at 15:49
-
Code–only answers aren't helpful, you should include an explanation of the code and what it does. And please do not reference w3schools, it's full of errors, use the language specification or MDN for examples and further information. – RobG Mar 22 '16 at 23:41
If you have a lot of date arithmetics, I can only strongly recommend the use of moment.js
.
Using this library, your code would be as short as moment().add(1, 'days').hours(8).startOf('hour')
.
moment.js works with 'moment' objects that wrap over JS dates to provide additional methods. The moment()
invocation returns a moment of the current datetime, thus being the moment.js equivalent to new Date()
.
From there we can use the moment.js methods, as add(quantity, unit)
that adds a duration to the previous date. All these manipulation methods return a modified moment, which mean we can chain them.
The hours()
methods is both a getter and a setter depending on its arguments ; here we provide it with a number, which mean we set the moment's hour part to 8. A call to .hours()
would have instead returned the current hour part.
startOf(unit)
returns a moment at the start of the unit, meaning it will set all lesser units to 0 : moment().startOf('day')
would return today's 00:00 am.

- 24,009
- 2
- 33
- 57
-
There is no moment.js tag, nor is it mentioned in the question therefore the answer should use plain js **and** explain how it works. – RobG Mar 22 '16 at 23:39
-
1Where do you pull these rules from? What is there to explain that isn't made obvious by the methods' names or a quick check to the provided link? A plain JS answer has already been provided by Rana and if you check its comments and edit you'll see I tried to help making it better. I'm all for constructive criticism but I don't think I can do much with your feedback. – Aaron Mar 22 '16 at 23:53