4

With the following code:

var today = new Date();
console.log(String(today));

Chrome and FF return: "Tue Dec 27 2016 10:55:35 GMT+0700 (SE Asia Standard Time)"

BUT, Safari returns: "Tue Dec 27 2016 10:55:35 GMT+0700 (ICT)"

I want Safari return the same result like Chrome and FF did. How can I do it?

Is this a bug of Safari?

NoName
  • 877
  • 12
  • 28
  • The output from [*Date.prototype.toString*](http://ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tostring) is entirely implementation dependent, there is nothing you can do to change the format, timezone string or any aspect of it. Just because one host is inconsistent with others does not make it wrong, just different. There are various libraries that do a reasonable job of detecting the user's time zone. They aren't 100% accurate so if that matters, don't rely on it. – RobG Dec 27 '16 at 08:56

2 Answers2

1

I want Safari return the same result like Chrome and FF did. How can I do it?

You will need to reimplement the Date.prototype.toString method. The built–in method is implementation dependent, so as long as it fits the fairly loose requirement of ECMA-262 (see below), it's conformant.

There are various libraries that do a reasonable job of detecting the host timezone offset and converting it to a time zone name, generally the IANA timezone name. Converting to other time zone names such as "SE Asia Standard Time" is more problematic as likely not everyone in that timezone calls it the same name or uses the same abbreviation.

Is this a bug of Safari?

No. The specification simply requires that Date.prototype.toString returns "an implementation-dependent String value that represents [the date] as a date and time in the current time zone using a convenient, human-readable form.", so anything that fits that is fine.

What is the difference between "SE Asia Standard Time" and "Indochina Time"? There is no standard for time zone names, nor for their abbreviations, so anything that is "convenient, human-readable" for someone is sufficient.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • You're right. This is different between FF/Chrome and Safari. So I think I can use offset value to detect timezone name. Both FF/Chrome and Safari return the same offset value. – NoName Dec 27 '16 at 10:36
  • @PhucNguyen—the offset value is based on the host settings. There is no standard for timezone names, and there may be multiple time zones with different names that share the same offset. – RobG Dec 27 '16 at 22:25
0

It seems that Safari has some timezone issues not only with the standard acronyms (SE Asia Standard Time vs. ITC), but also detecting in which timezone the user is. Here are some issues I found:

Why is Safari confused about Date.getDay() for DST start in Sydney, Aus time zone?

Safari Time Zone issue

Besides this, you can just add some code like the following

var today = new Date();
var x = String(today);

var ending;
for (var i = 0; i < x.length; i++) {
  if (x[i] == "(") {
    ending = i;
  }
}
x = x.slice(0, ending);
x += "(SE Asia Standard Time)";

console.log(x);

I think this will allow you at least to format it properly.

Community
  • 1
  • 1
sebasaenz
  • 1,917
  • 2
  • 20
  • 25
  • 1
    ICT is just one case in many case of Safari. Let's look at https://golang.org/src/time/zoneinfo_abbrs_windows.go?m=text. I think we should create a function to convert DST to STD time zone... – NoName Dec 27 '16 at 04:32
  • But the problem you've been dealing with is the last part between parentheses, right? I don't understand why DST would be a problem when the two hour's display both in Chrome/FF and Safari are numerically the same. – sebasaenz Dec 27 '16 at 04:42
  • yes, the issue here is that the TimeZone ID (between parentheses) is not matched with my expected when using Safari. Currently, I see Safari running on my Mac returns DST timezone id, not STD timezone id as FF/Chrome running on my Windows PC. So, I think it comes from STD and DST timezone....hmm – NoName Dec 27 '16 at 04:47
  • Oh, ok, now I get it! – sebasaenz Dec 27 '16 at 04:50
  • Is there any ways that I can get the timezone name from timezone offset value in javascript? let me ask Google Professor – NoName Dec 27 '16 at 04:52
  • That's a good possibility. I've been reading the documentation about date methods and it doesn't seem to exist a built-in method to convert date to STD time zone. – sebasaenz Dec 27 '16 at 04:59
  • you're right. We have to build manually a function to convert date to STD time zone. And I am thinking about using offset value to do that...See the answer of @user3119287 http://stackoverflow.com/questions/18246547/get-name-of-time-zone – NoName Dec 27 '16 at 05:15
  • yes, it seems the only way to do it with STD time zone ID's is manually – sebasaenz Dec 27 '16 at 05:23
  • The Google maps API requires that you know the latitude and longitude of the client reasonably accurately, you can't guarantee that in a general case. – RobG Dec 27 '16 at 08:55
  • I cannot use Google API in this case because my input parameter is a Date object, not latitude and longitude as Google API required. – NoName Dec 27 '16 at 10:38