mlwacosmos mlwacosmos - 1 year ago 135
Javascript Question

Javascript Dates : change time

Do you know an elegant way to change the time in a Date Object in javascript

The strangness is those setters that return Number object

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

date is a Number not a date..

so let's say I have a date
var date = new Date()

and I want to change time

Thank you

Answer Source
var date = new Date();
date.setHours( 0,0,0,0 );

setHours() actually has two effects:

  1. It modifies the object it is applied to
  2. It returns the new timestamp of that date object

So in your case, just create the object and set the time separately afterwards. You can then ignore the return value entirely, if you don't need it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download