-2

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

Bahubali Ak
  • 181
  • 3
  • 13

4 Answers4

1

You just need to use new Date()

new Date('2022-02-17')
Hao-Jung Hsieh
  • 516
  • 4
  • 9
1

You can do new Date('2022-02-2017').

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

Docs