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 OverflowTry 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)
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.
What's are best practices when dealing with dates, without time-of-day?
How to get current date without time as a date object?
[AskJS] Get difference between datetime without timezone reference
Question regarding dates and time zones
Videos
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:
Use the an alternate type like integer value, of milliseconds or days (since 1970-01-01), or string in
YYYYMMDDformat.This was combined with #1Use
Dateignoring time (as 00:00 local TZ). Convert from/to UTC when reading/writing to databaseUse
Datewith time as 00:00 UTC. Have to be careful not to mix withDatevalues in local TZ (e.g.now = new Date())Use
Datein local TZ, but convert to UTC when read/writing to database. This is a variant of #3.Create a
LocalDateclass that enforces midnight.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.