문제

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