C#转换字符串“ yyyy-mm-dd HH:MM:SSSSSSSSSS”到目前为止

发布于 2025-02-09 05:47:15 字数 610 浏览 2 评论 0原文

I have a string, as an example: "2022-04-17 14:46:31 UTC"

And I want to convert it to date time but I have got the following error:

字符串'2022-04-17 14:46:31 UTC'未被认为是有效的 dateTime.s有一个未知的单词,从索引'20'开始。

Notice that I have tried:

var date =  DateTime.Parse("2022-04-17 14:46:31 UTC");

and

DateTime dt;
DateTime.TryParseExact("2022-04-17 14:46:31 UTC", 
                       "yyyy-MM-dd hh:mm:ss 'UTC'", 
                       CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)

I have a string, as an example: "2022-04-17 14:46:31 UTC"

And I want to convert it to date time but I have got the following error:

The string '2022-04-17 14:46:31 UTC' was not recognized as a valid
DateTime.There is an unknown word starting at index '20'."

Notice that I have tried:

var date =  DateTime.Parse("2022-04-17 14:46:31 UTC");

and

DateTime dt;
DateTime.TryParseExact("2022-04-17 14:46:31 UTC", 
                       "yyyy-MM-dd hh:mm:ss 'UTC'", 
                       CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2025-02-16 05:47:15

continue

There are two things to be done:

  1. as mentioned in the comments: use HH for the 24 hour format
  2. adjust the result to an actual UTC time
DateTime.TryParseExact("2022-04-17 14:46:31 UTC", "yyyy-MM-dd HH:mm:ss 'UTC'", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
DateTime utc = new DateTime(dt.Ticks,  DateTimeKind.Utc);
凉栀 2025-02-16 05:47:15

根据此答案您需要将时区名称转换为时移。

因此,要首先,我们需要获取时区名称表到时区移动。我将其复制在这里并转换为字典。

完整的算法是:

  1. 查找时区名称
  2. find在字典中
  3. 替换时区名称用shift
  4. parse date date string

var dateString = "2022-04-17 14:46:31 UTC";
var lastWhiteSpace = dateString.LastIndexOf(' ');
var timeZoneName = dateString.Substring(lastWhiteSpace+1);
var correctDate = dateString.Replace(timeZoneName, TimeZoneToShift[timeZoneName]);
var date = DateTime.ParseExact(correctDate, "yyyy-MM-dd HH:mm:ss zzz", null);

public static Dictionary<string, string> TimeZoneToShift = new Dictionary<string, string>()
{
    {"ACDT", "-10:30"},
    {"ACST", "-09:30"},
    {"ADT", "+03:00"},
    {"AEDT", "-11:00"},
    {"AEST", "-10:00"},
    {"AHDT", "+09:00"},
    {"AHST", "+10:00"},
    {"AST", "+04:00"},
    {"AT", "+02:00"},
    {"AWDT", "-09:00"},
    {"AWST", "-08:00"},
    {"BAT", "-03:00"},
    {"BDST", "-02:00"},
    {"BET", "+11:00"},
    {"BST", "+03:00"},
    {"BT", "-03:00"},
    {"BZT2", "+03:00"},
    {"CADT", "-10:30"},
    {"CAST", "-09:30"},
    {"CAT", "+10:00"},
    {"CCT", "-08:00"},
    {"CDT", "+05:00"},
    {"CED", "-02:00"},
    {"CET", "-01:00"},
    {"CST", "+06:00"},
    {"EAST", "-10:00"},
    {"EDT", "+04:00"},
    {"EED", "-03:00"},
    {"EET", "-02:00"},
    {"EEST", "-03:00"},
    {"EST", "+05:00"},
    {"FST", "-02:00"},
    {"FWT", "-01:00"},
    {"GMT", "+00:00"},
    {"GST", "-10:00"},
    {"HDT", "+09:00"},
    {"HST", "+10:00"},
    {"IDLE", "-12:00"},
    {"IDLW", "+12:00"},
    {"IST", "-05:30"},
    {"IT", "-03:30"},
    {"JST", "-09:00"},
    {"JT", "-07:00"},
    {"MDT", "+06:00"},
    {"MED", "-02:00"},
    {"MET", "-01:00"},
    {"MEST", "-02:00"},
    {"MEWT", "-01:00"},
    {"MST", "+07:00"},
    {"MT", "-08:00"},
    {"NDT", "+02:30"},
    {"NFT", "+03:30"},
    {"NT", "+11:00"},
    {"NST", "-06:30"},
    {"NZ", "-11:00"},
    {"NZST", "-12:00"},
    {"NZDT", "-13:00"},
    {"NZT", "-12:00"},
    {"PDT", "+07:00"},
    {"PST", "+08:00"},
    {"ROK", "-09:00"},
    {"SAD", "-10:00"},
    {"SAST", "-09:00"},
    {"SAT", "-09:00"},
    {"SDT", "-10:00"},
    {"SST", "-02:00"},
    {"SWT", "-01:00"},
    {"USZ3", "-04:00"},
    {"USZ4", "-05:00"},
    {"USZ5", "-06:00"},
    {"USZ6", "-07:00"},
    {"UT", "+00:00"},
    {"UTC", "+00:00"},
    {"UZ10", "-11:00"},
    {"WAT", "+01:00"},
    {"WET", "+00:00"},
    {"WST", "-08:00"},
    {"YDT", "+08:00"},
    {"YST", "+09:00"},
    {"ZP4", "-04:00"},
    {"ZP5", "-05:00"},
    {"ZP6", "-06:00"},
};

According to this answer you need to convert time zone name to time shift.

So to do it first we need to get table of time zone name to time zone shift. I copy it from here and convert to dictionary.

And the full algorithm is:

  1. Find time zone name
  2. Find it shift in dictionary
  3. Replace time zone name with shift
  4. Parse date string

var dateString = "2022-04-17 14:46:31 UTC";
var lastWhiteSpace = dateString.LastIndexOf(' ');
var timeZoneName = dateString.Substring(lastWhiteSpace+1);
var correctDate = dateString.Replace(timeZoneName, TimeZoneToShift[timeZoneName]);
var date = DateTime.ParseExact(correctDate, "yyyy-MM-dd HH:mm:ss zzz", null);

public static Dictionary<string, string> TimeZoneToShift = new Dictionary<string, string>()
{
    {"ACDT", "-10:30"},
    {"ACST", "-09:30"},
    {"ADT", "+03:00"},
    {"AEDT", "-11:00"},
    {"AEST", "-10:00"},
    {"AHDT", "+09:00"},
    {"AHST", "+10:00"},
    {"AST", "+04:00"},
    {"AT", "+02:00"},
    {"AWDT", "-09:00"},
    {"AWST", "-08:00"},
    {"BAT", "-03:00"},
    {"BDST", "-02:00"},
    {"BET", "+11:00"},
    {"BST", "+03:00"},
    {"BT", "-03:00"},
    {"BZT2", "+03:00"},
    {"CADT", "-10:30"},
    {"CAST", "-09:30"},
    {"CAT", "+10:00"},
    {"CCT", "-08:00"},
    {"CDT", "+05:00"},
    {"CED", "-02:00"},
    {"CET", "-01:00"},
    {"CST", "+06:00"},
    {"EAST", "-10:00"},
    {"EDT", "+04:00"},
    {"EED", "-03:00"},
    {"EET", "-02:00"},
    {"EEST", "-03:00"},
    {"EST", "+05:00"},
    {"FST", "-02:00"},
    {"FWT", "-01:00"},
    {"GMT", "+00:00"},
    {"GST", "-10:00"},
    {"HDT", "+09:00"},
    {"HST", "+10:00"},
    {"IDLE", "-12:00"},
    {"IDLW", "+12:00"},
    {"IST", "-05:30"},
    {"IT", "-03:30"},
    {"JST", "-09:00"},
    {"JT", "-07:00"},
    {"MDT", "+06:00"},
    {"MED", "-02:00"},
    {"MET", "-01:00"},
    {"MEST", "-02:00"},
    {"MEWT", "-01:00"},
    {"MST", "+07:00"},
    {"MT", "-08:00"},
    {"NDT", "+02:30"},
    {"NFT", "+03:30"},
    {"NT", "+11:00"},
    {"NST", "-06:30"},
    {"NZ", "-11:00"},
    {"NZST", "-12:00"},
    {"NZDT", "-13:00"},
    {"NZT", "-12:00"},
    {"PDT", "+07:00"},
    {"PST", "+08:00"},
    {"ROK", "-09:00"},
    {"SAD", "-10:00"},
    {"SAST", "-09:00"},
    {"SAT", "-09:00"},
    {"SDT", "-10:00"},
    {"SST", "-02:00"},
    {"SWT", "-01:00"},
    {"USZ3", "-04:00"},
    {"USZ4", "-05:00"},
    {"USZ5", "-06:00"},
    {"USZ6", "-07:00"},
    {"UT", "+00:00"},
    {"UTC", "+00:00"},
    {"UZ10", "-11:00"},
    {"WAT", "+01:00"},
    {"WET", "+00:00"},
    {"WST", "-08:00"},
    {"YDT", "+08:00"},
    {"YST", "+09:00"},
    {"ZP4", "-04:00"},
    {"ZP5", "-05:00"},
    {"ZP6", "-06:00"},
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文