0

Is there any way to parse date in GMT format into date in UTC format using JS? Or everything needs to be parsed manualy?

Basically, I search for a way to transform first string into the second:

Wed, 23 Apr 2014 09:15:42 GMT

2014-04-23T12:15:42.046 UTC

Does anyone know how this can be achieved? Every useful answer / JSFiddle is highly appreciated and evaluated.

Thank you.

amenoire
  • 1,892
  • 6
  • 21
  • 34

3 Answers3

3

Please Check this out hope this answer helps u in some manner.

Link

Try this

    var now = new Date(); 
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
Community
  • 1
  • 1
sagar43
  • 3,341
  • 3
  • 29
  • 49
1

A very close to your output:

console.log(new Date("Wed, 23 Apr 2014 09:15:42 GMT").toISOString())

Prints: 2014-04-23T09:15:42.000Z (Z means UTC)

closure
  • 7,412
  • 1
  • 23
  • 23
1

If you don't mind using a library, you can use moment.js for getting the exact format that you want:

moment.utc('Wed, 23 Apr 2014 09:15:42 GMT').format('YYYY-MM-DD[T]HH:mm:ss.SSS [UTC]');

This is the output:
2014-04-23T09:15:42.000 UTC

Please note that the SSS part of the date will be 000 since the original date doesn't have fractional seconds.

Now, if you use a new Date object, it will remove the deprecation warning and you can get this:

moment.utc(new Date()).format('YYYY-MM-DD[T]HH:mm:ss.SSS [UTC]');

Output:
2014-04-24T14:10:38.395 UTC

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
  • SSS can be ignored, right? Can I just write format('YYYY-MM-DD[T]HH:mm:ss [UTC]') ? – amenoire Apr 24 '14 at 13:23
  • Yes, you can leave off the `SSS` part if you don't want the fractional seconds in the conversion. – Cameron Tinker Apr 24 '14 at 13:26
  • Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info. Can I remove this message somehow or resolve this? – amenoire Apr 24 '14 at 13:39
  • If you use a new `Date` object instead of a hard coded date string, it will remove the deprecation warning. I'm assuming you want the current time and not a hard coded date. I hope this helps. – Cameron Tinker Apr 24 '14 at 14:13