This question already has answers here:
Why isn't "2016-02-16" equal to "2016-02-16 00:00"?
(5 answers)
Closed 6 years ago.
I am creating a date with new Date(). When I do this, it is subtracting a day. Here is the code:
var dateString = "2016-04-10";
var date = new Date(dateString);
// date = Sat Apr 09 2016 18:00:00 GMT-0600 (MDT)
What do I misunderstand? Why is the date not Apr 10 2016? How do I make this work properly?
Your timezone is GMT-6 (as revealed by the GMT-0600 (MDT) in the output you've provided). Therefore the date which gets generated is offset by -6 hours. In this case, midnight minus 6 hours is 6PM on the previous day.
If you call date.toISOString(), you'll see that the UTC time is "2016-04-10T00:00:00.000Z" as expected.
Related
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 1 year ago.
Why when I create without space in javascript console is to the yesterday date at 9:00PM, but when I write with a space in the end the date and hour is ok?
new Date('2021-07-20')
Wrong: Mon Jul 19 2021 21:00:00
new Date('2021-07-20 ')
Correct: Tue Jul 20 2021 00:00:00
2021-07-20 is a valid ISO time. It gets parsed as 20th July 2021 at midnight UCT. It then gets converted to local time for you which (in Porto Alegre) is three hours earlier.
2021-07-20 is not a valid ISO time, so it hits your JS engine's implementation-specific parser for non-standard date formats.
You shoud use , between them like:
const d = new Date(2018, 11, 24, 10, 33, 30);
And remember JavaScript counts months and days from 0 to 11 just like indexes.
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Google Apps Script date format issue (Utilities.formatDate)
(2 answers)
Closed 2 years ago.
Can anyone tell me why this code would produce two different dates?
let now = new Date(); // today's date (1/2/2021)
Logger.log(now); // Sat Jan 02 09:42:47 GMT-08:00 2021
Logger.log(new Date(now.getTime()-(2*1000*60*60*24))); // Thu Dec 31 09:42:47 GMT-08:00 2020
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY")); // 12/31/2021
Why would Utilities.formateDate() change the date from 12/31/2020 to 12/31/2021?
******** SOLUTION *********
Change the date format from "MM/d/YYYY" to "MM/d/yyyy".
It's just a simple formatting problem. Look here
This:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY"));
Should be this:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/yyyy"));
In other words just make the capital Y's lower case y's and you're there.
This question already has answers here:
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 3 years ago.
why its showing past day date
var date = new Date('2020-01-01');
console.log(date)
Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time)
Because of your timezone settings (Easter Standard time is GMT -0500 therefore 5 hours before 2020-01-01).
Javascript Date object are timestamps - they merely contain a number
of milliseconds since the epoch. There is no timezone info in a Date
object. Which calendar date (day, minutes, seconds) this timestamp
represents is a matter of the interpretation (one of to...String
methods).
Basically it is the toString method that converts the date to your local timezone.
Source
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
Observe the following code and it's output
let str = '2019-06-21';
let dateObj = new Date(str);
console.log(dateObj)
> Thu Jun 20 2019 19:00:00 GMT-0500
The Date object is a day behind than what I specified.
What is a robust way to amend this?
Creating a function to modify the 'DD' portion seems hacky.
I've settled on decrementing the Date object after constructing, but is there a better way?
Why does this behavior happen?
Specify timezone. Currently its defaulting to GMT.
str = '2019-06-21T00:00:00-07:00';
dateObj = new Date(str);
>> Fri Jun 21 2019 00:00:00 GMT-0700 (Pacific Daylight Time)
The date object created is in UTC so when you console it you get the day, month and year in your Timezone
let str = '2019-06-21';
let dateObj = new Date(str);
console.log(dateObj.toDateString())
// expected "Thu Jun 20 2019" based on your timezone.
console.log(dateObj.toUTCString())
// expected "Fri, 21 Jun 2019 00:00:00 GMT"
To create a DateTime based on your timezone, you can either give your timezone or do the following
let d = new Date(Date.now())
d.setDate(21)
d.setHours(0) // if you care to start the day at 00 hrs
d.setMinutes(0) // if you care to start the day at 00 mins
d.setSeconds(0) // if you care to start the day at 00 seconds
console.log(d.toDateString())
This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I'm using javascript new Date format. It is working well in android and windows but when I use it on iphone it outputs different result. How can I be able to solve the problem?
var rawGetDate = "2019-02-01 00:00:00";
var dateF = new Date(rawGetDate.replace(' ', 'T'));
console.log(dateF);
OutPut in Androaid: Tue Jan 01 2019 00:00:00 GMT+0600
OutPut in Iphone: Mon Dec 01 2018 00:00:00 GMT+0500