-2

I need to convert date/time values.

For example I want to convert this "2019-11-21 09:53:10 Etc/GMT" to a value with the same format as this "2020-02-13T15:24:47.977Z".

If you can provide the resulting equivalent value of the given example, I can make that as reference so I can already find the correct code for the conversion.

Or else if you can provide the exact javascript code for the conversion, that is better. I am using 'moment' and 'moment-timezone'.

d_air
  • 631
  • 1
  • 4
  • 12
  • I am downvoting because there is no attempt here. You literally are telling us to write your code. Id suggest you write your own formatter and if you cant do that, post your work and we can see and augment it as necessary. – Fallenreaper Feb 19 '20 at 16:54
  • I suggested in the question that it's enough for me that someone can give the correct resulting value, and I can already write my code. Right now I can write the code for conversion, but I'm not sure if the resulting value is correct. – d_air Feb 19 '20 at 16:57
  • what you are looking for is different from what you are asking, you dont ask a follow up question when your question has been answered wasting the precious time of others whiles you put in no effort – eboakyegyau Feb 19 '20 at 17:04
  • @eboakyegyau I did ask a follow up question to your answer immediately as I saw your answer. Maybe I will also downvote your answer later. ;) – d_air Feb 19 '20 at 17:20

4 Answers4

1

You just have to pass the your date string in this format 2019 11 21 09:53:10 GMT

let date = new Date('2019 11 21 09:53:10 GMT');
console.log(date.toISOString());
Nitesh Phadatare
  • 315
  • 1
  • 2
  • 12
  • is Etc/GMT the same as GMT? – d_air Feb 19 '20 at 17:00
  • 1
    They represent the same instant in time only when the offset is 0. https://stackoverflow.com/questions/7303580/understanding-the-etc-gmt-time-zone – Nitesh Phadatare Feb 19 '20 at 17:03
  • I actually got this date from Apple server. And one thing that confuses me is the Etc/GMT. usually the time format is something like this Etc/GMT+1 but the value from Apple has only Etc/GMT. So this confuses me on how to convert this. – d_air Feb 19 '20 at 17:05
  • I think, I'm going to accept your answer. But I'll verify everything first. thank you. – d_air Feb 19 '20 at 17:12
  • `new Date('2019-11-21 09:53:10').toLocaleString("en-US", {timeZone: "Etc/GMT"})` I this is the right way. This will accept time in `Etc/GMT` format. But this will not give you result in ISO format – Nitesh Phadatare Feb 19 '20 at 17:13
  • The code throws an error in Safari. '2019 11 21 09:53:10 GMT' is not a format supported by ECMA-262 so parsing is implementation dependent, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Feb 20 '20 at 01:46
  • 1
    @NiteshPhadatare—that is not "the right way". That format is not supported by ECMA-262, it's not parsed correctly by Safari. – RobG Feb 20 '20 at 01:55
1

"2019-11-21 09:53:10 Etc/GMT" is not a format supported by ECMA-262 so parsing is implementation dependent and can't be relied on, e.g. in Safari:

new Date("2019-11-21 09:53:10 Etc/GMT");

returns an invalid Date.

If you just want to convert "2019-11-21 09:53:10 Etc/GMT" to a valid ISO 8601 format that is also supported by ECMA-262, then there is no need for a Date object at all, just reformat the string slightly.

let timestamp = '2019-11-21 09:53:10 Etc/GMT'
let iso = timestamp.substr(0, 19).replace(' ','T') + 'Z';

console.log(iso);

// or more tolerant of time part
let timestamp2 = '2019-11-21 09:53:10.123 Etc/GMT'
console.log( timestamp2.replace(' ','T').replace(/ Etc.*$/,'Z') );

// Or
console.log( timestamp2.split(' ').slice(0,2).join('T') + 'Z');

// Or
let [d, t, rest] = timestamp2.split(' ');
console.log(`${d}T${t}Z`);

There are many ways to reformat the string without transforming to a Date and back to string. Of course if you have a string like:

'2019-11-21 09:53:10 Etc/GMT-4'

then you will need to manually parse it to get UTC as "Etc/GMT" offsets have the opposite sense to common offsets (i.e. they are +ve west and -ve east).

RobG
  • 142,382
  • 31
  • 172
  • 209
  • The sample value '2019-11-21 09:53:10 Etc/GMT' that I provided is coming from the Apple server. I'm not sure yet if there is chance that Apple will give something like this '2019-11-21 09:53:10.45 Etc/GMT' with extra milliseconds. In this case, this answer would discard the extra milliseconds. But still, this answer will also work in my use case. – d_air Feb 20 '20 at 11:02
  • @d_air—updated. The point is that you can fairly simply reformat the string without bothering with parsing to Date and then formatting with the various issues that involves. – RobG Feb 20 '20 at 11:32
0

As commented by Nitesh in his answer, Etc/GMT is the same as GMT when the offset is zero or GMT+0. If I have realized this at the beginning, I may not have ask this question.

I've tried the following, and they results with "2019-11-21T09:53:10.000Z".

    let date1 = new Date('2019 11 21 09:53:10 GMT+0');
    console.log(date1.toISOString());

    let date2 = new Date('2019 11 21 09:53:10 GMT');
    console.log(date2.toISOString());

    let date3 = new Date('11/21/2019 09:53:10 GMT+0000');
    console.log(date3.toISOString());

    let date4 = new Date('11/21/2019 09:53:10 GMT');
    console.log(date4.toISOString());

The conversion will not work if I use my original sample value which contains '-' and 'Etc/'.

The following will also results to "2019-11-21T09:53:10Z"

    let date5 = moment.tz("2019-11-21 09:53:10", "Etc/GMT+0");
    console.log(date5.format());

    let date6 = moment.tz("2019-11-21 09:53:10", "Etc/GMT+0");
    console.log(date6.utc().format());

    let date7 = moment.tz("2019-11-21 09:53:10", "Etc/GMT");
    console.log(date7.format());

    let date8 = moment.tz("2019-11-21 09:53:10", "Etc/GMT");
    console.log(date8.utc().format());

I did not find any solution that does not alter the original value. So I will need to modify the original value first before creating the Date object.

    let a = "2019-11-21 09:53:10 Etc/GMT";  // sample value
    let b = a.replace(/-/g, " ").replace("Etc/", ""); // result: "2019 11 21 09:53:10 GMT"
    let c = new Date(b);
    console.log(c.toISOString()); // 2019-11-21T09:53:10.000Z

Alternatively, without creating a new Date object is also acceptable to me.

    let d = "2019-11-21 09:53:10 Etc/GMT";  // sample value
    let b = d.replace(" Etc/GMT", "Z").replace(" ", "T"); // result: "2019-11-21T09:53:10Z"
    console.log(b); 
d_air
  • 631
  • 1
  • 4
  • 12
-1

Try this out

 var d = new Date(); 
 var n = d.toUTCString();

You can have a refernce here https://www.geeksforgeeks.org/how-to-convert-a-javascript-date-to-utc/

eboakyegyau
  • 225
  • 1
  • 13
  • Even if the answer has the correct format. How do I know if the date/time value is correct? – d_air Feb 19 '20 at 16:13
  • first of all how do you plan on getting your date/time? the time being correct or wrong will only depend or what your reference clock is. – eboakyegyau Feb 19 '20 at 16:19
  • your question is talking about conversion of time only so might have to rephrase to the exact thing you need answer for – eboakyegyau Feb 19 '20 at 16:21
  • The example given above is enough for me. I got that exact string value. I want to convert it. But i'm not sure if my conversion result is correct. – d_air Feb 19 '20 at 16:21
  • if thats correct for you can accept it but for your time being correct or not will depend on where you are getting your time from and to which reference clock its being referenced to – eboakyegyau Feb 19 '20 at 16:24
  • If you have a Javascript Date you can use the function above to get UTC but it looks like you are looking for ISO Format and not UTC.. You need to make sure the date given is in an acceptabe date format, or convert it to one. for example: `new Date()` just returns now. So when you get the correct formatting to parse it into a Date object things will be easy. – Fallenreaper Feb 19 '20 at 16:52
  • @Fallenreaper Yes, I'm sorry with my wordings. I already edited my question to ISO instead of UTC. – d_air Feb 19 '20 at 17:09
  • I may have considered your answer if it was like this: var d = new Date('2019 11 21 09:53:10 GMT'); var n = d.toUTCString(); That will not need a reference clock. The result is always the same wherever you are in the world. Furthermore, that answer should give me a hint Etc/GMT is the same as GMT. It may not be the exact answer, but it would have help me get the final correct answer. – d_air Feb 19 '20 at 22:48