I have date in the format of "YYYY-MM-DD" i need to convert it into Mon Feb 21 2022 05:30:00 GMT+0530 (India Standard Time) format
Asked
Active
Viewed 3,184 times
-2
-
`new Date(string)`? – VLAZ Feb 17 '22 at 10:41
-
new Date("2022-02-17").toString()?? – cmgchess Feb 17 '22 at 10:41
-
Does this answer your question? [How do you convert a JavaScript date to UTC?](https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc) – pasine Feb 17 '22 at 10:42
-
More answers https://stackoverflow.com/questions/5619202/parsing-a-string-to-a-date-in-javascript and https://stackoverflow.com/questions/17545708/parse-date-without-timezone-javascript – pasine Feb 17 '22 at 10:45
-
1How is 2022-02-17 equal to 2022-02-21 [05:30:00]? – KooiInc Feb 17 '22 at 10:45
-
@cmgchess thanks dude working fine – Bahubali Ak Feb 17 '22 at 10:46
-
@cmgchess How does `.toString()` help in converting to date object? – phuzi Feb 17 '22 at 10:48
-
What did you try? What didn't work? – Pavan J Feb 17 '22 at 10:48
-
@cmgchess I just wanted to convert "YYYY-MM-DD" date to above asked format. – Bahubali Ak Feb 17 '22 at 10:49
-
1@phuzi i guess i didnt read the question properly. thought he needed to format that way – cmgchess Feb 17 '22 at 10:56
-
@BahubaliAk Not sure how 17th Feb becomes 22nd Feb! Guessing that's a typo – phuzi Feb 17 '22 at 10:57
4 Answers
1
To answer the question in the title, you can simply just initialize a Date object by using the string you have
const date = new Date("2022-02-17");
Now formatting it into something else can be a tedious to do. Using library such as date-fns would be easier.
Javascript has a native method closer to what you want:
new Date("2022-02-17").toLocaleString('hi-IN', { timeZone: "Asia/Kolkata" })
will return: '17/2/2022, 5:30:00 am'
.
Now formatting it to something else IMO should be in a different question since the title of this question is already answered.

captainskippah
- 1,350
- 8
- 16
0
I would recommend using Luxon for date manipulation in Javascript.
import { DateTime } from 'luxon';
const dt = new DateTime.fromISO("2022-02-17");
const formatted = dt.toLocaleString(DATETIME_FULL);
In your question the date you want to convert it to is 4 days ahead. If this isn't a typo, you can also add days to the date object like this:
const addDays = dt.plus({ days: 4 });

user3536141
- 53
- 5