0

I have what I am sure is a silly question but I cannot find the answer. I have date string with GMT offsets such as:

"Sun May 28 2017 07:30:00 GMT+0800",

However my current location is GMT+1000, so when I do:

var x = new Date("Sun May 28 2017 07:30:00 GMT+0800");

i get

"Sun May 28 2017 09:30:00 GMT+1000 (AEST)"

How do I cast a string with a GMT offset into a Date object that correctly shows the time? In this example I would need it to be 07:30:00.

deceze
  • 510,633
  • 85
  • 743
  • 889
ncbl
  • 1,295
  • 2
  • 12
  • 19
  • [.toLocaleString()](https://www.w3schools.com/jsref/jsref_tolocalestring.asp) or [toLocaleString()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) – alessandrio May 24 '17 at 02:08
  • The "correct" time in time zone +1000 is 09:30. – RobG May 24 '17 at 02:10
  • "Correctly" is rather vague… the date *is* correct as is; but you want to display it in a *different timezone than your own*, namely the original timezone. – deceze May 24 '17 at 02:10
  • Unfortunately that gives me the current offset time, ie: 09:30 not 07:30 – ncbl May 24 '17 at 02:10
  • @deceze I would like it to show 07:30 not 09:30. So, not convert the GMT offset to what my local machines location – ncbl May 24 '17 at 02:11
  • I know, see the duplicate. Javascript itself has very limited capabilities for timezone conversions *which are widely supported*; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat. – deceze May 24 '17 at 02:13
  • Please see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results?s=1|8.0469) and [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|21.0035) – RobG May 24 '17 at 02:14
  • @deceze—I don't think that's a duplicate. The OP doesn't want to change the timezone, s/he wants to ignore it. – RobG May 24 '17 at 02:17
  • exactly, this is NOT a duplicate. I want to ignore the offset and show the time. So it the output would be would be "Sun May 28 2017 07:30:00" regardless. – ncbl May 24 '17 at 02:20
  • Well, a `Date` object is timezone independent for all intents and purposes *until* you want to get information out of it, at which point you have the choice to get the information in the local timezone or UTC; getting the information in other timezones, including the timezone the `Date` was originally constructed with, is not really something you can do. So it boils down to formatting the date in a specific timezone → duplicate. – deceze May 24 '17 at 02:20
  • @ncbl—write your own function to parse the string as local values and ignore the offset, 3 lines of code should do it (*split* on space and colon, convert month name to number, then *new Date* with the parts to get to: `new Date(2017,4,28,7,30)`. Note months are zero indexed so 4 is May. – RobG May 24 '17 at 02:59

0 Answers0