Javascript Date() returns wrong date [duplicate] - javascript

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

Related

i want to convert date Data to date? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Converting dates in JavaScript
(5 answers)
Closed 2 years ago.
I get data from open weather api and when i convert it i got the full date i want to extract only the hours :
sunrise: 1607412091
i try this method
var daterise= data.sys.sunrise;
var exactdate= new Date(daterise*1000);
document.getElementById("rise").innerHTML="Sunrise:"+ exactdate;
and i got the full date
Tue Dec 08 2020 08:21:31 GMT+0100 (UTC+01:00)
I want just the hour
i want to extract the hours from the date ??
cosnt hours = exactdate.getHours()

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

How can we change the date format in JavaScript? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Format a date string in javascript
(7 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
Given the data 2018-04-28T18:38:23Z
When I use Date(2018-04-28T18:38:23Z)
This gives me Sat Apr 28 2018 18:38:23 GMT-0500
I want to remove that GMT thing, but how can I do with simply without
writing some long code to parse it out?
JavaScript Date has some methods you could use to craft the String you want.
Here's something close, you could play around with the methods (getHours/Minutes/Seconds), but if you want exactly that without the timezone, I would recommend parsing out or doing some String manipulation:
var d = new Date();
console.log(d.toDateString() + " " + d.toLocaleTimeString());
// Output:
// Sat Apr 28 2018 4:58:05 PM

Showing correct date from API [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 5 years ago.
I'm getting crazy with showing a date correctly to the client:
From the API I receive data.expiry_date and its value is: 2017-09-06T23:59:59Z
The client to show that data looks like this:
var date = new Date(data.expiry_date);
$('#expiry_date').val(`${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`);
The result is a day before than expected: it should be 06/09/2017 but it show 07/09/2017.
Basically the date value from the action var date = new Date(data.expiry_date); is: Thu Sep 07 2017 01:59:59 GMT+0200 (CEST).
How can I get rid of it?
You're not in the same time zone.
2017-09-06T23:59:59Z is UTC
which is same as
Thu Sep 07 2017 01:59:59 GMT+0200 (CEST)

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