Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top