51

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();
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
Mimi Lauren
  • 640
  • 1
  • 6
  • 11
  • 4
    linked, but not as much a duplicate is http://stackoverflow.com/questions/20188317/how-to-compare-the-date-part-alone-from-a-date-time-value –  Aug 22 '14 at 10:57
  • 6
    Have a look at the answer to the above question; `date.setHours(0,0,0,0)` should be enough. – Salman A Aug 22 '14 at 11:00
  • Have a look at [momentjs](http://momentjs.com/docs/#/get-set/) Great library for manipulating dates – Albert Aug 22 '14 at 11:01
  • Also [date-fns](https://date-fns.org) has `startOfToday` which does what you want, and `isToday` which is also useful. – Matt Jun 10 '21 at 23:58

6 Answers6

54

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)

Icaro Martins
  • 307
  • 12
  • 22
trrrrrrm
  • 11,362
  • 25
  • 85
  • 130
  • 5
    w3schools links make me a sad snarf. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours – snarf Jul 27 '21 at 23:38
29

Try this:

var nowDate = new Date(); 
var date = nowDate.getFullYear()+'/'+(nowDate.getMonth()+1)+'/'+nowDate.getDate(); 

Note: Adjust format as you want, like reorder day, month, year, remove '/' and get combined date etc.

immayankmodi
  • 8,210
  • 9
  • 38
  • 55
8

or use this:

dateToEpoch(new Date().toLocaleDateString())
Hamed
  • 5,867
  • 4
  • 32
  • 56
Dawesi
  • 568
  • 3
  • 9
1

I tried using javascript. this method returns the current date in "DD/MM/YYYY" format.

getCurrentDate() {
  const t = new Date();
  const date = ('0' + t.getDate()).slice(-2);
  const month = ('0' + (t.getMonth() + 1)).slice(-2);
  const year = t.getFullYear();
  return `${date}/${month}/${year}`;
}
1

in 2023 you can use the new Intl js global object new Intl.DateTimeFormat(['ban', 'id']).format(new Date) MDN page

Hillel
  • 11
  • 4
1

Solution:

const date = new Date();
date.setHours(0, 0, 0, 0);

First answer (dateTicks % 86400000) is bad, you can ensure:

const date = new Date();
console.log(date + ' - source datetime');

const date1 = new Date(date);
date1.setHours(0, 0, 0, 0);
console.log(date1 + ' - today via setHours (good)');

const date2ticks = date.getTime();
const date2 = new Date(date2ticks - date2ticks % 86400000);
console.log(date2 + ' - today via remainder (bad)');

console.log(date1 === date2 ? 'two ways are the same!' : 'two ways are the different!'); 
Dmitry Shashurov
  • 1,148
  • 13
  • 11