0

I found out that on a mac my script is acting different than on a windows. On both systems i am using chrome as the browser, i don't know how to explain my problem properly so ill show you. Below the Javascript function it converts a json time to a javascript date.

function getDateFromJSON(value) {
   var retvalue = new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
   return retvalue
}

When i use this function with a json date string on a windows pc as shown bellow the output is: Sat Oct 22 1988 00:00:00 GMT+0200 (W. Europe Daylight Time)

When i use the same function with a mac the output is: Fri Oct 21 1988 23:00:00 GMT+0100 (CEST)

The code i used on both mac and windows is:

console.log(getDateFromJSON("/Date(593474400000)/"));

Can someone please help me explain how i can fix this?

SheetJS
  • 22,470
  • 12
  • 65
  • 75
Josef32
  • 38
  • 1
  • 5
  • The timezone name in parentheses comes from the operating system, and there's no common standard for that. Why is that a problem? – Barmar Jul 27 '13 at 01:27
  • 2
    The other difference is that the two machines have different timezone settings. They're one timezone apart. – Barmar Jul 27 '13 at 01:28
  • im developing a web application and some customers are using mac, and dates are changing like in the example: Mac substracts on day from the original when this user save's, the date has changed. (no one noticed at first) – Josef32 Jul 27 '13 at 01:29
  • 1
    It's not subtracting a day. 23:00 Friday in CEST is the same time as 00:00 Saturday in WEDT. – Barmar Jul 27 '13 at 01:31
  • It has nothing to do with Mac vs PC... – Matt Johnson-Pint Jul 27 '13 at 03:09

1 Answers1

2

Both dates are the same (after adjusting for timezones)

If you want to standardize, use toUTCString:

> new Date(" Sat Oct 22 1988 00:00:00 GMT+0200 (W. Europe Daylight Time)").toUTCString()
'Fri, 21 Oct 1988 22:00:00 GMT'
> new Date("Fri Oct 21 1988 23:00:00 GMT+0100 (CEST)").toUTCString()
'Fri, 21 Oct 1988 22:00:00 GMT'
SheetJS
  • 22,470
  • 12
  • 65
  • 75
  • The time i am hoping to get is the european, so is there a way to convert the mac time (cest) to (w. Europe Daylight Time)? – Josef32 Jul 27 '13 at 01:36
  • @Josef32 if you want to force western european daylight time, you can either adjust the times by adding an offset (add 2 hours to the time, then call toUTCString and replace GMT with `(W. Europe Daylight Time)`) OR use a proper library like https://github.com/mde/timezone-js – SheetJS Jul 27 '13 at 01:48
  • 2
    Thanks all for your help, the problem was that chrome on a mac doesn't recognize daylight saving time. The solution of Nirk to use timezone.js worked. – Josef32 Jul 27 '13 at 03:18
  • @Josef32 - That's just blatantly false. Chrome on a mac absolutely *does* recognize daylight saving time. – Matt Johnson-Pint Oct 27 '13 at 05:49