简体   繁体   English

从字符串c#解析DateTime

[英]Parse DateTime from string c#

I have date that I get from incoming API call: Wed, 6 Mar 2019 14:39:49 +0300 我有来自传入API调用的日期: Wed, 6 Mar 2019 14:39:49 +0300

I need to parse this string to DateTime. 我需要将此字符串解析为DateTime。 For this I'm using the following code: 为此,我使用以下代码:

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                     new string[] { "ddd, dd MMM yyyy HH:mm:ss zzzz" },
                     CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

But as a result I have error: 但结果我有错误:

String 'Wed, 6 Mar 2019 14:39:49 +0300' was not recognized as a valid DateTime. 字符串'Wed,6 Mar 2019 14:39:49 +0300'未被识别为有效的DateTime。

What am I doing wrong? 我究竟做错了什么? How can I resolve this? 我该如何解决这个问题?

I see 2 things; 我看到两件事;

  1. You should use d specifier instead of dd specifier since your single digit day number does not have a leading zero . 您应该使用d说明符 ,而不是dd符,因为你的个位数的日数没有 前导零
  2. There is no zzzz as a custom format specifier. 没有zzzz作为自定义格式说明符。 You should use zzz specifier instead. 您应该使用zzz说明符

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                     new string[] { "ddd, d MMM yyyy HH:mm:ss zzz" },
                     CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

But honestly, if your strings have a UTC Offset value, I would suggest parse it to DateTimeOffset instead since a DateTime instance does not have offset part and using zzz specifiers is not recomended as stated on MSDN. 但老实说,如果您的字符串具有UTC偏移值,我建议将其解析为DateTimeOffset因为DateTime实例没有偏移部分,并且不推荐使用zzz说明符,如MSDN中所述。

With DateTime values, the "zzz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. 使用DateTime值,“zzz”自定义格式说明符表示本地操作系统的时区与UTC的签名偏移量,以小时和分钟为单位。 It does not reflect the value of an instance's DateTime.Kind property. 它不反映实例的DateTime.Kind属性的值。 For this reason, the "zzz" format specifier is not recommended for use with DateTime values. 因此,建议不要将“zzz”格式说明符与DateTime值一起使用。

To parse DateTimeOffset , 要解析DateTimeOffset

DateTimeOffset.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                           new string[] { "ddd, d MMM yyyy HH:mm:ss zzz" },
                           CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

Now you can use it's .DateTime and/or .Offset properties separately if you want. 现在,如果需要,您可以单独使用它的.DateTime和/或.Offset属性。

Change "ddd, dd MMM yyyy HH:mm:ss zzzz" to "ddd, d MMM yyyy HH:mm:ss zzzz" 将“ddd,dd MMM yyyy HH:mm:ss zzzz”改为“ddd,d MMM yyyy HH:mm:ss zzzz”

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                     new string[] { "ddd, d MMM yyyy HH:mm:ss zzzz" },
                     CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

You might be looking for 你可能正在寻找

DateTime myDate = DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300",
                                      "ddd, d MMM yyyy HH:mm:ss zzz",
                                      CultureInfo.InvariantCulture, 
                                      DateTimeStyles.AdjustToUniversal);

Refer: https://stackoverflow.com/a/10426999/4373895 This would also help you.Thanks. 参考: https//stackoverflow.com/a/10426999/4373895这也可以帮到你。谢谢。

Datetime.ParseExact function just like below code withh help you. Datetime.ParseExact函数就像下面的代码一样帮助你。 One thing needs to be changed is instead of dd MMM yyyy use d MMM yyyy. 需要改变的一件事是代替dd MMM yyyy使用d MMM yyyy。

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", "ddd, d MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

d MMM yyyy而不是ddd MMM yyyy

date = DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", "ddd, d MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

The format for the UTC offset is zzz not zzzz and expects it to have a colon ( : ) to separate the hours from the minutes (such as +03:00 ). 对于UTC偏差格式zzzzzzz ,我们期望它有一个冒号( : ),以小时从几分钟(如分离+03:00 )。 As well as the dd is for leading zero day of month but you have a single digit for the day of month. 除了dd是指月份的前导零日,但是你有一个月的单个数字。 In that case use d . 在那种情况下使用d

DateTime time = Convert.ToDateTime("Wed, 6 Mar 2019 14:39:49 +0300");
string ti = time.ToString(@"ddd, dd MMM yyyy HH:mm:ss zzzz");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM