Why is this an invalid date? [duplicate] - javascript

This question already has answers here:
What are valid Date Time Strings in JavaScript?
(2 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 1 year ago.
Im trying to parse a timestamp string to date object, but it keeps telling me its an invalid date.
Mon Jun 21 10:14:54 CEST 2021
Where new Date('Mon Jun 21 10:14:54 CEST 2021') return an invalid date error. Date.parse and toDate also return the same error. I used http://natty.joestelmach.com/try.jsp# to test if an online parser was able to recognize it and it did. What can be wrong here?

Related

JS Date YYYY-MM-DD Returning incorrect value [duplicate]

This question already has answers here:
Incorrect date shown in new Date() in JavaScript
(3 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
I have HTML Date inputs that return a string in the format of 'YYYY-MM-DD'
I then want to put this into the Date Constructor so that I can get it converted into ISOFormat (for use of Mongo Querying)
However, the results are unexpected.
Doing new Date('2020-06-25') returns "Wed Jun 24 2020 20:00:00 GMT-0400 (Eastern Daylight Time)". Note that this is the day prior. Why does this happen and how can I change this to get the current date? THanks
When you create a new Date object in JavaScript, it expects the argument to be in GMT. It then converts it to your local timezone GMT-0400, which explains why its 4 hours off. You can set the timezone by appending it to the date string: new Date('2020-06-25 GMT-0400') should give you "Thu Jun 25 2020 00:00:00 GMT-0400".

Convert Date to javascript format [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am trying to convert this date into Mon Nov 26 2018 10:32:04 GMT (I am getting this data from the Api so i can't make changes to it)
I assume it is considering 26 as months thats why it is showing it as invalid date
Can Anyone help me with this. How to convert that date into the expected output i specified.
How to get
var d = new Date("26-11-2018 10:32:04")
return d; //Error: Invalid Date
expected Output: Mon Nov 26 2018 10:32:04 (IST)
Use moment.js to parse the date.
moment("26-11-2018 10:32:04", "DD-MM-YYYY HH-mm-ss").toDate()
Alternatively, if you really don't want to use moment for whatever reason, you can use regex magic.
new Date("26-11-2018 10:32:04".replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, "$3-$2-$1T$4:$5:$6Z"))
This is not a robust as #Yevgen answer but it also much simpler.
All I'm doing is removing the - and flipping the day and month values
const items = "26-11-2018 10:32:04".split('-')
new Date(`${items[1]} ${items[0]} ${items[2]}`)
This works for personal projects but I highly recommend using moment.js

convert/parse YYYY-MM-DD object to string ot to JavaScript Date object [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
I have the following object 2019-12-01. When I do:
console.log(typeof 2019-12-01);
The output is: NaN
So I want to convert this to string like this "2019-12-01" or to date object like:
Sun Dec 01 2019 02:00:00 GMT+0200 (Eastern European Standard Time)
I tried so many thing, but I cannot figure it out.
Any help will be appreciated.
Why typeof, you can just create a date object
console.log(new Date("2019-12-01"))
console.log(new Date("2019-12-01").toISOString().replace(/T.*/, ""))

Is there a way to convert javascript Date object to string but retain the format as displayed when console.log is used on the Date object? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Create an ISO date object in javascript
(5 answers)
Closed 3 years ago.
I want to convert the date object to string but I want it in the same format as it is displayed when I do console.log(new Date()) which is something like '2019-05-21T11:55:39.496Z' and not the usual extended format we get when we do console.log(new Date().toString()) which is something like this 'Tue May 21 2019 17:28:51 GMT+0530 (India Standard Time)'.
console.log(new Date().toISOString())
2019-05-21T12:03:50.601Z

convert javascript string to date object [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I am getting an error when I try to format the following date.
var d = new Date('Jan 01 2019 12:00AM');
I am getting Invalid Date
You need to remove AM from your line new date.
var d = new Date('Jan 01 2019 12:00');

Categories

Resources