How to get current date without time?
Asked Answered
C

7

59

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();
}
Choctaw answered 22/8, 2014 at 10:55 Comment(4)
linked, but not as much a duplicate is #20188817Rameau
Have a look at the answer to the above question; date.setHours(0,0,0,0) should be enough.Almond
Have a look at momentjs Great library for manipulating datesPropagation
Also date-fns has startOfToday which does what you want, and isToday which is also useful.Cromer
L
60

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)

Linguist answered 22/8, 2014 at 11:10 Comment(1)
w3schools links make me a sad snarf. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Logography
D
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.

Descendible answered 22/8, 2014 at 11:3 Comment(1)
Zero Padding: var date = nowDate.getFullYear()+'/'+('0'+(nowDate.getMonth()+1)).substring(0,2)+'/'+('0'+nowDate.getDate()).substring(0,2);Dulcy
M
9

or use this:

dateToEpoch(new Date().toLocaleDateString())
Midlands answered 15/7, 2022 at 16:13 Comment(0)
O
6

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!'); 
Obduliaobdurate answered 21/8, 2023 at 11:55 Comment(0)
I
3

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

Interbrain answered 18/4, 2023 at 8:41 Comment(0)
P
2

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}`;
}
Pazia answered 1/12, 2021 at 5:45 Comment(0)
B
-1

Try this: for people who are here to get just anydate without time.

let date = div.toLocaleString("en-us", { year: 'numeric', month: 'numeric', day: 'numeric' });
Barnard answered 14/11, 2023 at 6:6 Comment(2)
How does this answer the question? The snippet doesn't even run without an errorOutfit
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Soper

© 2022 - 2024 — McMap. All rights reserved.