LoginSignup
0
0

More than 1 year has passed since last update.

【JavaScript】UTC形式の時刻をJST形式(日本時間)に変換する。

Last updated at Posted at 2023-03-24

使用するAPI

今回は、GitHub APIからJSONを取得します。

APIのエンドポイント

以下のエンドポイントで、リポジトリ内のプルリクエストの履歴が取得できます。

https://api.github.com/repos/GitHubのユーザー名/リポジトリ名/pulls

取得結果の一部がこちらです。

"created_at": "2023-03-23T08:05:05Z",

"created_at"はプルリクエストを作成した時刻で、UTC形式になっています。
こちらのUTC形式のJST形式(日本時間)にしていきます。

サンプルコード

以下が、形式を変換するコードになります。

const url = "https://api.github.com/repos/GitHubのユーザー名/リポジトリ名/pulls"
fetch(url)
  .then(response => response.json())
  .then(data => {
   console.log(data[0].created_at)
   const dataTime = data[0].created_at;
   const dt = new Date(dataTime);
   cosnole.log(dt.toLocaleString());
}

5行目で取得できるデータはこちらです。

2023-03-23T08:05:05Z

6~8行目で変換するとこうなります。

2023/3/23 17:05:05

このように、JST形式(日本時間)に変換することができました。
簡単にはなりましたが以上になります。

0
0
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
0
0