So I want to display a date with no particular attention to time of day. There is also nothing messing with the date. I am on a MacBook Pro.
If I input numbers into the Date constructor the following below appears.
I want to show April 28, 1993 for example.
var objDate = new Date(1993, 3, 28),
dateString = objDate.toLocaleDateString(undefined, {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
timeZoneName: 'long'
});
console.log(dateString)
//Chrome Outputs: "Wednesday, April 28, 1993, 12 AM Mountain Daylight Time"
//Firefox Outpus: "Tuesday, April 27, 1993, 6 PM Mountain Daylight Time"
let nowDate = new Date(1993, 3, 28);
console.log(nowDate.toString())
//Chrome Outputs: "Wed Apr 28 1993 00:00:00 GMT-0600 (Mountain Daylight Time)"
//Firefox Outputs: "Wed Apr 28 1993 00:00:00 GMT+0000 (UTC)"
console.log(nowDate.toLocaleString())
//Chrome Outputs: "4/28/1993, 12:00:00 AM"
//Firefox Outputs: "4/27/1993, 6:00:00 PM"
So now the dates are showing both the 27th and the 28th, even though I want it to show just the 28th.
But! If I change the numbers to a string format like the following code. It is still inconsistent.
var objDate = new Date("1993-04-28"),
dateString = objDate.toLocaleDateString(undefined, {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
timeZoneName: 'long'
});
console.log(dateString)
//Chrome Outputs: "Wednesday, April 27, 1993, 12 AM Mountain Daylight Time"
//Firefox Outpus: "Tuesday, April 27, 1993, 6 PM Mountain Daylight Time"
let nowDate = new Date("1993-04-28");
console.log(nowDate.toString())
//Chrome Outputs: "Wed Apr 27 1993 00:00:00 GMT-0600 (Mountain Daylight Time)"
//Firefox Outputs: "Wed Apr 28 1993 00:00:00 GMT+0000 (UTC)"
console.log(nowDate.toLocaleString())
//Chrome Outputs: "4/27/1993, 12:00:00 AM"
//Firefox Outputs: "4/27/1993, 6:00:00 PM"
Now here is the last bit of code.
If I input a time of day into the string format, like noon, it turns out fine as per to show which day it is.
EDIT: I just checked this method in Safari, and it outputs Invalid Date.
var objDate = new Date("1993-04-28 12:00"),
dateString = objDate.toLocaleDateString(undefined, {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
timeZoneName: 'long'
});
console.log(dateString)
//Chrome Outputs: "Wednesday, April 28, 1993, 12 PM Mountain Daylight Time"
//Firefox Outpus: "Wednesday, April 28, 1993, 6 AM Mountain Daylight Time"
let nowDate = new Date("1993-04-28");
console.log(nowDate.toString())
//Chrome Outputs: "Wed Apr 28 1993 12:00:00 GMT-0600 (Mountain Daylight Time)"
//Firefox Outputs: "Wed Apr 28 1993 12:00:00 GMT+0000 (UTC)"
console.log(nowDate.toLocaleString())
//Chrome Outputs: "4/28/1993, 12:00:00 PM"
//Firefox Outputs: "4/28/1993, 6:00:00 AM"
So now here are the questions in particular.
Why do the methods with different date inputs, sometimes set a different day? What are the browsers doing differently in each one of the listed cases? I thought each of the methods I used would be locally based and not UTC based.
EDIT: I might just have to use one of the libraries just to handle all the inconsistencies. Although is there a reasonably small library? I found dayjs and it looks small enough.