Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current date without time?

Tags:

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(); } 
like image 253
Mimi Lauren Avatar asked Aug 22 '14 10:08

Mimi Lauren


People also ask

How can I get only the local date?

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.


1 Answers

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)

like image 179
trrrrrrm Avatar answered Oct 21 '22 18:10

trrrrrrm