Daniyar Changylov
Daniyar Changylov

Reputation: 568

How to get date to ISOString with GMT timezone

I get date

Fri Dec 20 2019 00:00:00 GMT+0600 (East Kazakhstan Time) 

But when I convert to ISOString I get

2019-12-19T18:00:00.000Z

and I guess this convert does not consider timezone..

Also I tried convert with moment-timezone & same result.

moment("Fri Dec 20 2019 00:00:00 GMT+0600 (East Kazakhstan Time)").tz('Asia/Bishkek').toISOString()  


If I get date with timezone I should convert with timezone:
I should get

2019-12-20T00:00:00.000Z

not

2019-12-19T18:00:00.000Z

If I'm not right and I make a mistake correct me


UPDATED

I found solution which solves my problem

moment(this.data.start.getTime() - this.data.start.getTimezoneOffset()*60*1000).toISOString()

which convert from GMT +0 to GMT +6 timezone
And now new question:
How to make more simple above code using momentjs.. Or this is enough?

Upvotes: -3

Views: 2184

Answers (1)

RobG
RobG

Reputation: 147363

I get date Fri Dec 20 2019 00:00:00 GMT+0600 (East Kazakhstan Time) But when I convert to ISOString I get 2019-12-19T18:00:00.000Z

The timestamps represent exactly the same moment in time. There is no error here.

If I get date with timezone I should convert with timezone. I should get 2019-12-20T00:00:00.000Z

No you should not. You should get 2019-12-19T18:00:00.000Z.

If I'm not right and I make a mistake correct me

You are not right.

toISOString is always UTC. If you want ISO 8601 format and your local timezone, use the built–in parser and the default moment formatting:

let s = 'Fri Dec 20 2019 00:00:00 GMT+0600 (East Kazakhstan Time)';

// Built–in parser, not recommended
let d = new Date(s);
// Format with moment.js
console.log(moment(d).format());

// Modify string for parsing with moment.js
let e = s.substr(0,33).replace('GMT','');
// Parse with moment.js
let m = moment(e, 'ddd MMM DD YYYY HH:mm:ss ZZ');
// Format with moment.js
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Upvotes: 1

Related Questions