Help us understand the problem. What is going on with this article?

JavaScriptのDateの挙動(Safari)

More than 1 year has passed since last update.

Dateの挙動がChromeとSafariで違うことに今日気がつきました...

var dt = new Date('2018-11-28T11:30:30+0900');

ブラウザごとの挙動

Chrome

Chrome
ふつーに動きますね。

Safari

Safari
あれ?ダメです。

仕様を調査

Date - JavaScript | MDNを見てみます。

構文

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

new Date(dateString);のdateStringはISO8601形式を認識するとのことなんですけどねぇ...

対応策

結局この方法で対応しました。(2018-11-29コメントを受けて修正)

var at = '2018-11-28T11:30:30+0900';
var dt = new Date(at.replace(/([\+|\-])([0-9]{2})([0-9]{2})$/,"$1$2:$3"));

タイムゾーンの+hhmm部分を+hh:mmにするとSafariでも正しく認識してくれるとのことなので、+0900を+09:00に変換するようにしました。

Firefoxでも確認しましたが大丈夫でした。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away