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 3 years have passed since last update.

【C#】タイムゾーンを指定した日時を拡張ISO 8601形式で取得する

Last updated at Posted at 2021-01-30

やりたいこと

C#で、UTCの日時が入ったDateTime型変数から、2021-01-30T15:47:02+09:00のような時差情報を含む拡張ISO 8601形式の文字列に変換する。

実装

// using System;
DateTime dateTimeUtc = DateTime.UtcNow;
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
DateTime tstDateTime = TimeZoneInfo.ConvertTimeFromUtc(dateTimeUtc, tst);
string iso8601DateTime = tstDateTime.ToString("yyyy-MM-dd'T'HH:mm:sszzz");

補足

DateTime.ToStringの書式について

DateTime.ToString メソッド | Microsoft Docsより転記

// This example displays the following output to the console:
//       d: 6/15/2008
//       D: Sunday, June 15, 2008
//       f: Sunday, June 15, 2008 9:15 PM
//       F: Sunday, June 15, 2008 9:15:07 PM
//       g: 6/15/2008 9:15 PM
//       G: 6/15/2008 9:15:07 PM
//       m: June 15
//       o: 2008-06-15T21:15:07.0000000
//       R: Sun, 15 Jun 2008 21:15:07 GMT
//       s: 2008-06-15T21:15:07
//       t: 9:15 PM
//       T: 9:15:07 PM
//       u: 2008-06-15 21:15:07Z
//       U: Monday, June 16, 2008 4:15:07 AM
//       y: June, 2008
//
//       'h:mm:ss.ff t': 9:15:07.00 P
//       'd MMM yyyy': 15 Jun 2008
//       'HH:mm:ss.f': 21:15:07.0
//       'dd MMM HH:mm:ss': 15 Jun 21:15:07
//       '\Mon\t\h\: M': Month: 6
//       'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00

TimeZoneInfo.FindSystemTimeZoneByIdで指定可能な値

調べても見つからず。
ご存じの方、コメントください。
参考:TimeZoneInfo.FindSystemTimeZoneById(String) メソッド | Microsoft Docs

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?