I try to set a date to midnight to simplify my date manipulation, for this I wrote this part of code:
var now = new Date();
today = now.setHours(0,0,0,0);
console.log(now, today);
I'm surprised to see now
contains a Date object and today
a timestamp. This brings errors when I want to use getMonth() or other date's functions. It's paintful to recreate a Date object with the timestamp.
Is it normal? How can I fix this?
(Feel free to update my post to correct my bad english :)
The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance.
You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.
setTime() The setTime() method sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
Is it normal?
Yes
How can I fix this?
You are assigning the return value of now.setHours(0,0,0,0)
to today
.
Maybe what you are looking for is something like this:
var now = new Date();
var today = new Date();
today.setHours(0,0,0,0);
In this way, setHours
is acting upon the value you wish to have the hours set on. This is the primary manner of using setHours
.
Other details
SET_LOCAL_DATE_VALUE
function found in chromium's date.js is assigning the value into the first argument.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With