0

I am using JavaScript Date function, new Date).toString() and getting result like this Wed Jun 28 2017 10:31:09 GMT+0530 (India Standard Time) , But I require the GMT value like this

Result: Wed Jun 28 2017 10:31:09 GMT+0530 (India Standard Time)

Require: GMT+05:30

How can I get this?

  • hint: `'Wed Jun 28 2017 10:31:09 GMT+0530 (India Standard Time)'.replace(/GMT\+(\d\d)(\d\d)/, 'GMT+$1:$2')` – Jaromanda X Jun 28 '17 at 05:06
  • Actually a better duplicate could be found searching on how to split a string – mplungjan Jun 28 '17 at 05:08
  • use moment.js better for all kind of date function – Durga Jun 28 '17 at 05:08
  • @mplungjan—thinking about it further, does the OP simply want "GMT+0530"? If so, then *getTimezoneOffset* may be involved. – RobG Jun 28 '17 at 05:09
  • @mplungjan I need string like this - "GMT+05:30" – Shubham Dexbytes Jun 28 '17 at 05:16
  • Probably a duplicate of [*How to get UTC offset in javascript (analog of TimeZoneInfo.GetUtcOffset in C#)*](https://stackoverflow.com/questions/9149556/how-to-get-utc-offset-in-javascript-analog-of-timezoneinfo-getutcoffset-in-c) – RobG Jun 28 '17 at 06:40

2 Answers2

0

you can use something like this:

"Wed Jun 28 2017 10:31:09 GMT+0530 (India Standard Time)".match(/GMT\+[0-9]+/)[0];
Dij
  • 9,761
  • 4
  • 18
  • 35
  • This assumes that Date.prototype.toString returns a string in that format in every implementation. However, the return value is implementation dependent, so you can't rely on that. And you certainly can't rely on the sign always being "+". – RobG Jun 28 '17 at 06:40
0

Try this:

console.log(new Date().toString().match(/([A-Z]+[\+-][0-9]+)/)[1]);
Jay Patel
  • 2,341
  • 2
  • 22
  • 43