new Date() is adding hours [duplicate] - javascript

This question already has answers here:
What is the best way to initialize a JavaScript Date to midnight?
(10 answers)
Closed 2 years ago.
new Date('2020-10-25')
Sun Oct 25 2020 03:00:00 GMT+0300 (East Africa Time)
I want the hour to be 00:00:00 instead of 03:00:00. I don't want to pass the hour in the date, 2020-10-25 0:0.
Since the question is closed, I can't post my answer, hence I'm writing here,
new Date('10/25/2020') Using / instead of - working as expected.

you can set Hours to 0
var a = new Date();
a.setHours(0, 0, 0, 0);

Related

new Date() vs. Utilities.formatDate(new Date()) [duplicate]

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.

how to set time zone in javascript new Date() [duplicate]

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

Why is new Date() removing a day? - Javascript [duplicate]

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.

Setting the date in date field sets it with one day before [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 6 years ago.
I am using the date input type to display/get dates from a form in HTML:
<input type="date">
To set the date from JavaScript I do:
myInput.valueAsDate = new Date();
This works fine. But if I want to set another date object, for example:
myInput.valueAsDate = new Date(1995, 0, 1);
It displays 12/31/1994–which is exaclty one day before the 1995 New Year eve. If I add 24 as hours argument, the date is displayed correctly, but the date object itself is obviously 2nd of January, 1995, which is not a good solution to this problem.
console.log(myInput.valueAsDate = new Date(1995, 0, 1));
console.log(myInput.valueAsDate);
<input type="date" id="myInput">
I get this in the console:
Sun Jan 01 1995 00:00:00 GMT+0200 (EET)
Sat Dec 31 1994 02:00:00 GMT+0200 (EET)
Is this a browser issue? Are there any workarounds/solutions?
And finally, I do not want to use any plugin to display/get the dates but I want to use the natative date input element (at least, the question is about it :-)).
I reproduced this in Chromium and Chrome. Firefox seems not to support the date type inputs yet.
It's the timezone difference read this.
Apparently when using new Date() you use the current time zone and valueAsDate takes a GMT dateTime
Changing you code to something like this
console.log(myInput.valueAsDate = new Date(1995, 0, 1,12));
should work
Update 1
console.log(myInput.valueAsDate = new Date(Date.UTC(1995, 0, 1));
Should work on all timezones.

javascript new Date() issue [duplicate]

This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 8 years ago.
I'm facing a date issue with new Date() function. May i know why is this weired output is coming.
new Date(1984, 12, 13)
Sun Jan 13 1985 00:00:00 GMT-0500 (EST)
Here the month should be December but it results me with Jan.
January is 0 in javascript , so javascript is counting up and is adding the date. That's why you get this result.

Categories