javascript new Date() issue [duplicate] - javascript

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.

Related

Date wrong in JS [duplicate]

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.

new Date() is adding hours [duplicate]

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);

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

Javascript Date() returns wrong date [duplicate]

This question already has answers here:
javascript Date timezone issue
(3 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 4 years ago.
I've been looking for someone with the same problem here but I could not find. Whenever I call Date() on my hidden value it seems to be subtracting one day. I am using Chrome.
I am passing a hidden value to my html:
<input type="hidden" name="start_date" value="2018-07-29" class="start-date" id="id_start_date">
It's correct when I call:
var hiddenDate = $('#id_start_date')[0].value;
alert(hiddenDate); # 2018-07-29
But incorrect if I call:
var date = new Date(hiddenDate);
alert(date); # Sat Jul 28 2018 19:00:00 GMT-0500 (Central Daylight Time)
What am I doing wrong and how can I fix it? Thanks

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.

Categories