tags:

views:

41

answers:

3

What is the simplest way to obtain an instance of new Date() but set the time at midnight?

A: 

i saw this

<SCRIPT LANGUAGE="JavaScript">
var t= new Date();
t.setHours(0);
t.setMinutes(0);
t.setSeconds(0);
t.setMilliseconds(0);
writeln("The date and time is " + t + "<BR>");
</SCRIPT>

here: link. (might be helpful)

orolo
+1  A: 

Give Date() only parameters for Year, month and day. The Time will be set to O:00:00 in this case.

new Date(2010, 9 , 8);

...will set the Date to october 8th 2010 0:00

I would'nt say it's the best, but as long as it will do the same like the other proposals, for me it's the best, because the shortest ^^

Dr.Molle
+4  A: 

The setHours method can take optional minutes, seconds and ms arguments, for example:

var d = new Date();
d.setHours(0,0,0,0);

That will set the time to 00:00:00.000 of your current timezone, if you want to work in UTC time, you can use the setUTCHours method.

CMS
Brilliant! Exactly what I was looking for.
Sixty4Bit