Question

I want to take an arbitrary javascript date object and strip out the time portion. In .NET you would use the DateTime object's Date property

What is the javascript equivalent? I've seen people doing this:

var now = new Date();
var justDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());

Is this the preferred way? Should I throw this code in a utils module or is there a more simple way of doing this?

I'm using moment.js and jquery- any solution using either library is certainly welcome.

Était-ce utile?

La solution

You can use setHours():

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

All JavaScript dates have time information. This will just set the time portion to 00:00.000 local time. This can be useful when you want to compare dates.

Autres conseils

With moment.js you could use moment(0,"HH").toDate()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top