I'm trying to get the current date without the time and store it in a variable, within JavaScript. It needs to be without time as I'm converting it to an epoch date, with which I will use to measure the past 24 hours (if date is within 24 hours then it will be displayed). The problem is that with the added time, it doesn't match as within the last 24 hours.
e.g. it returns the date as the following when converted to epoch: 1408704590485
I want it to be like 1408662000000
I'm not to sure how to do this.
Code - How the current days epoch date is currently being stored -
var epochLoggingFrom; var epochLoggingTo; $(document).ready(function () { epochLoggingFrom = dateToEpoch(new Date()); epochLoggingTo = dateToEpoch(new Date()); }
dateToEpoch function -
function dateToEpoch(thedate) { return thedate.getTime(); }
Using LocalDate class Similar to the Joda-Time library, Java 8 java. time package included a LocalDate class to represent a date without a time-zone. To obtain the current date in the specified time zone, you can specify a Zone ID to the LocalDate. now() method.
Try this:
function dateToEpoch(thedate) { var time = thedate.getTime(); return time - (time % 86400000); }
or this:
function dateToEpoch2(thedate) { return thedate.setHours(0,0,0,0); }
Example : http://jsfiddle.net/chns490n/1/
Reference: (Number) Date.prototype.setHours(hour, min, sec, millisec)
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