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)

Answer from trrrrrrm on Stack Overflow
Discussions

What's are best practices when dealing with dates, without time-of-day?
4 is the usual standard. More on reddit.com
🌐 r/AskProgramming
6
1
May 19, 2022
How to get current date without time as a date object?
You could try something like the following. formatDate(prop("Due Date"), "L") == formatDate(now(), "L") That will simply compare the due date and current date when formatted as MM/DD/YYY. More on reddit.com
🌐 r/Notion
8
5
August 29, 2020
[AskJS] Get difference between datetime without timezone reference
Not possible if you only have the 2 datetimes like that. Maybe you have more info on the api data that you receive which you can use to figure out the correct timezone of each datetime More on reddit.com
🌐 r/javascript
15
13
November 12, 2023
Question regarding dates and time zones
Had the same issue, our DB and servers are set to UTC but in browser they're set to user timezone, which JS always assumes. To ensure JS knows what timezone the time you're providing is in, append it to the end of your timestamp. For example: new Date(myTimestamp + " UTC"). Yep, looks horrific but is a valid and recommended solution apparently. Welcome to JavaScript! Also be sure to check Safari support as the wah Safari handles dates is very messed up. More on reddit.com
🌐 r/learnjavascript
17
2
August 10, 2023
🌐
GitHub
github.com › moment › moment › issues › 3455
How to represent dates (without time) and times (without date)? · Issue #3455 · moment/moment
September 19, 2016 - A Moment.js object (like its underlying Date object) always represents an exact point in time. Sometimes, however, I just want to store a date (say 2016-09-19). This is not a point in time, but a calendar day. Depending on my time zone, ...
Author   DanielSWolf
🌐
TutorialsPoint
tutorialspoint.com › how-to-remove-time-from-date-typescript
How to remove Time from Date TypeScript?
September 4, 2023 - The toLocaleDateString() method returns a string representing the date portion of the Date object in the local time zone. It does not include the time portion. By default, it returns a string in the format "MM/DD/YYYY" (e.g., "05/05/2023"). ...
🌐
Reddit
reddit.com › r/askprogramming › what's are best practices when dealing with dates, without time-of-day?
r/AskProgramming on Reddit: What's are best practices when dealing with dates, without time-of-day?
May 19, 2022 -

What is best practice when dealing with date values without time, in JavaScript? My concern is subtle off-by-one bugs caused by local Time Zone (TZ) offset (e.g. +5 hours), when doing date math.

JavaScript's built-in Date type represents a date and time not just the date. Internal representation is an integer of microseconds since 1970-01-01 00:00:00 UTC. Other languages, like Python, have separate Date and DateTime types. Java 8 introduced LocalDate.

You also have things like: new Date('5/18/2020') is local TZ (in US), but new Date('2022-05-18') is UTC. Same with Date.parse(string). And the time zone on most servers in UTC, whereas on the browser side the time zone will vary.

The date values will be used for simple date math in code and will be stored in a SQL database.

Possibilities:

  1. Use the an alternate type like integer value, of milliseconds or days (since 1970-01-01), or string in YYYYMMDD format.

  2. This was combined with #1

  3. Use Date ignoring time (as 00:00 local TZ). Convert from/to UTC when reading/writing to database

  4. Use Date with time as 00:00 UTC. Have to be careful not to mix with Date values in local TZ (e.g. now = new Date())

  5. Use Date in local TZ, but convert to UTC when read/writing to database. This is a variant of #3.

  6. Create a LocalDate class that enforces midnight.

  7. Use a library. js-joda has LocalDate.

I am leaning towards #3 and #6. Some code I am writing:

class LocalDate extends Date {
  // Error if time isn't midnight local TZ
  // Do not accept string in ISO format
  constructor(date?: Date|number|string)

  // Convert to 00:00 UTC
  // To be used before write to database
  toUtc(): Date

  // Only return date.  Also affects toJSON()
  toISOString(): string

  // Returns today's date at 00:00 Local TZ
  static today(): LocalDate
  // Set time to midnight local TZ, without error check.
  static fromDateTime(date: Date|number): LocalDate
  // Convert to local TZ.  Error if not 00:00 UTC.
  static fromUtc(date: Date|number): LocalDate
}

UPDATE:

Various edits.

🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-date-without-time
How to get a Date without the Time in JavaScript | bobbyhadz
March 6, 2024 - The method changes the date's time components in place. The setHours method takes values for the hours, minutes, seconds and milliseconds as parameters. We set all values to 0 to get a date without the time.
🌐
SourceFreeze
sourcefreeze.com › home › how to get a date without the time in javascript
How to get a Date without the Time in JavaScript - Source Freeze
December 20, 2023 - The Date object in JavaScript comes with several methods that allow us to manipulate dates. One of these methods is toISOString(), which returns the date in the format of a string representing the date and time.
Find elsewhere
🌐
OpenReplay
blog.openreplay.com › common-date-time-operations-without-moment-js
Common date/time operations without Moment.js
May 23, 2023 - In conclusion, the JavaScript Date object is a powerful tool for working with date and time in JavaScript without relying on external libraries such as Moment.js. We have covered various ways to create and manipulate Date objects, including using the number of milliseconds since the epoch, the Date Time string, and date and time components.
🌐
DEV Community
dev.to › shubhampatilsd › removing-timezones-from-dates-in-javascript-46ah
Removing Timezones from Dates in Javascript - DEV Community
April 23, 2023 - I finally thought that this would be a great solution: store the output of .toLocaleString() and convert it to a Date again on the client, all without the timezone. However, when I implemented the solution in my React Native app, it didn't work. It kept telling me that I had passed an invalid date into the Date constructor. I searched on the internet. Then, I found this on the MDN Web Docs: This gave me a hint as to why the code worked in my browser but not my React Native app. The JS in Firefox was running on a different runtime than React Native. Since JavaScript is so uncoordinated, JS code typically runs in a specific environment called an engine.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-create-date-without-timezone
How to Create a Date without Timezone in JavaScript | bobbyhadz
March 6, 2024 - This is because my time zone is 3 hours ahead of Coordinated Universal time (UTC). To create a Date without the timezone, we called the toISOString() method on the Date object and removed the character Z from the ISO string.
🌐
W3Schools
w3schools.com › js › js_date_methods.asp
JavaScript Date Methods
The syntax is always Date.now(). UTC methods use UTC time (Coordinated Universal Time).
🌐
date-fns
date-fns.org
date-fns - modern JavaScript date utility library
date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
🌐
Bugfender
bugfender.com › blog › javascript-date-and-time
The Definitive Guide to JavaScript Date and Time | Bugfender
February 18, 2025 - The most common way to create a ... no argument is passed​ · To get the current date, you can use the new Date() constructor without passing any arguments....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › now
Date.now() - JavaScript | MDN
// This example takes 2 seconds to run const start = Date.now(); console.log("starting timer..."); // Expected output: "starting timer..." setTimeout(() => { const ms = Date.now() - start; console.log(`seconds elapsed = ${Math.floor(ms / 1000)}`); // Expected output: "seconds elapsed = 2" }, 2000);
🌐
Toptal
toptal.com › developers › software › definitive-guide-to-datetime-manipulation
How to get the Timestamp And Date in Javascript: JS DateTime | Toptal®
December 2, 2020 - Programming languages contain useful constructs to make our lives easier. The Date object is one such thing. It offers convenient methods to get the current time in JavaScript, store a date in a variable, perform date arithmetic, and format the date based on the user’s locale.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-get-current-date-todays-date-in-js
JavaScript Get Current Date – Today's Date in JS
November 7, 2024 - In this article, we learned about various approaches for obtaining the current date using JavaScript alone or with an external JavaScript library.