1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

toLocaleDateString()の和暦の挙動

1
Posted at

toLocaleDateString

Mac版Chromeでの平成

new Date() して引き算したりとかいろいろしていた西暦・和暦変換ですが、

今現在(2019/04/22)のJavascriptでは toLocaleDateString("ja-JP-u-ca-japanese", {era:"long", year:"numeric"}) のようにすることで手軽に取得できます。

new Date().toLocaleDateString("ja-JP-u-ca-japanese", {era:"long", year:"numeric"});
"平成31年"

Mac版Chromeでの令和

…が!

今現在、Mac版Chromeでは新元号「令和」を認識していないみたいです。

new Date(2020,0,1).toLocaleDateString("ja-JP-u-ca-japanese", {era:"long", year:"numeric"});
"平成32年"

↑平成32年になっちゃってる。

MacOSの領分なのかJSなのかChromeなのか分かりませんが、

更新されるまでは使えないようです。

Node.jsでの令和

…しかも!

Node.jsではtoLocaleDateString("ja-JP-u-ca-japanese")は機能しないみたいです。

index.js
dateString = new Date(2020,0,1).toLocaleDateString("ja-JP-u-ca-japanese", {era:"long", year:"numeric"});
console.log(dateString)
CE 2020

ちなみにnpm i -g nodeを実行して、バージョンはv11.14.0です。

懸念

ということはNuxtなどのサーバーサイドレンダリングでも機能しないんでしょうか。

-> 軽く試したら「平成32年」が表示されました。

ReactNativeみたいなJSをバンドルするタイプのアプリも怪しい(試してない)。

引き算タイプのほうが安全なのかも…。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?