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.

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top