6

I have a Date format like this "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)"

I want to convert that above format to this format 2020-04-20T00:00:00.000Z

Actually I tried this JSON.stringify(new Date("Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)")) while using this am getting the output one day before 2020-04-19T18:30:00.000Z

so please anyone help me to convert this date format "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)" like this 2020-04-20T00:00:00.000Z

Thanks in Advance.

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
Raghul SK
  • 1,256
  • 5
  • 22
  • 30
  • 1
    Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time) and 2020-04-19T18:30:00.000Z are the same moment in time, they just use different offsets. 2020-04-20T00:00:00.000Z is Fri Apr 20 2020 05:30:00 GMT+0530. – RobG Apr 20 '20 at 10:31

2 Answers2

14

Your date seems to be a standard string representation of new Date(), you can get the desired format by using new Date().toISOString()

console.log(new Date().toString())
console.log(new Date().toISOString())

// To create it from string
const dateStr = "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)"
console.log(new Date(dateStr).toISOString())
Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
  • am getting one day before like this 2020-04-19T18:30:00.000Z – Raghul SK Apr 20 '20 at 08:52
  • I want the output like this bro 2020-04-20T00:00:00.000Z – Raghul SK Apr 20 '20 at 08:55
  • JS date is based on the timezone of your computer, if you want the string like above, you can take the date from date object, and add time string `00:00:00` manually. Or you can use a library like moment.js – Anurag Srivastava Apr 20 '20 at 09:08
  • @AnuragSrivastava—the timezone settings of the host are irrelevant for this question. "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)" has a timezone offset, it will be used to determine the equivalent UTC time when it's parsed. It actually represents 2020-04-19T18:30:00.000Z, which is what *toISOString* returns. – RobG Apr 20 '20 at 10:26
  • @RobG Ok I get it, hopefully you can provide the correct solution – Anurag Srivastava Apr 20 '20 at 10:27
  • 1
    @AnuragSrivastava—your answer is fine, I was commenting on your comment. :-) – RobG Apr 20 '20 at 10:28
  • @RobG There's no way to add the offset to get the required time? – Anurag Srivastava Apr 20 '20 at 10:30
  • That requires parsing the original string and specifically ignoring the offset. Not difficult (3 or 4 lines of code), but "[*how to parse a date*](https://stackoverflow.com/search?q=%5Bjavascript%5D+how+to+parse+a+date)" has been asked a million times before. :-) – RobG Apr 20 '20 at 11:03
  • @RobG Yes I was thinking something of that sort – Anurag Srivastava Apr 20 '20 at 11:07
  • Is there any way to convert vice versa like normal string 5pm into 2022-04-20T17:00:00.000Z format? – Lex V Apr 29 '22 at 12:29
1

Anurag Srivastava's answer shows how you should parse the string and format it in the required format (given that the string is in one of the two formats supported by ECMA-262 and considering Why does Date.parse give incorrect results?).

Note that "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)" is the same instant in time as "2020-04-19T18:30:00.000Z". The first string is offset from UTC by 5 hr 30 min, so the equivalent UTC time is 5 hr 30 min earlier, which means the date is the previous day.

You haven't given a reason why you want to treat it as UTC and not consider the offset, so I don't think you should.

However, if you do have a good reason to parse it as UTC and ignore the supplied offset, then you can either:

  1. Modify the input string to set the offset as +0 and parse it using the built–in parser
  2. Parse the string yourself and treat it as UTC

let s = "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)";

// #1 Modify the input string, setting the offset to +0
let d = new Date(s.replace(/GMT.*$/,'GMT+0000')).toISOString();

console.log(d.toISOString());

// #2 Bespoke parser
function parseAsUTC(s) {
  let months = ['jan','feb','mar','apr','may','jun',
                'jul','aug','sep','oct','nov','dec'];
  let b = s.split(/\W/);
  return new Date(Date.UTC(b[3], months.indexOf(b[1].toLowerCase()),
                           b[2], b[4], b[5], b[6]));
}

console.log(parseAsUTC(s).toISOString());
RobG
  • 142,382
  • 31
  • 172
  • 209