简体   繁体   English

使用格式 'dd.MM.yyyy HH:mm:ss.fff z' 将字符串转换为日期

[英]Convert string to date with format 'dd.MM.yyyy HH:mm:ss.fff z'

My string of date is '2018-12-30T18:30:00.000Z' and i am unable to convert it to datetime.我的日期字符串是“2018-12-30T18:30:00.000Z”,我无法将其转换为日期时间。

I have tried我试过了

DateTimeOffset.ParseExact(DOCDate, "dd.MM.yyyy HH:mm:ss.fff z", CultureInfo.InvariantCulture);

The reason your code does now work, is that the date in your string is in yyyy-MM-ddTHH:mm:ss.fffZ format, and the format you specify to ParseExact is dd.MM.yyyy HH:mm:ss.fff z .您的代码现在可以工作的原因是字符串中的日期采用yyyy-MM-ddTHH:mm:ss.fffZ格式,而您指定给ParseExact的格式为dd.MM.yyyy HH:mm:ss.fff z That way the date could not be parsed.这样就无法解析日期。

DateTimeOffset.Parse("2018-12-30T18:30:00.000Z"); seems to work OK.似乎工作正常。

  string s="2018-12-30T18:30:00.000Z";
   DateTime da=     DateTime.ParseExact(s, "yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture);
   Response.Write(da.ToString());

Try this尝试这个

You can use DateTime.TryParse with DateTimeStyles.AdjustToUniversal to have the exact same time, or DateTimeStyles.AssumeUniversal to adjust to the Local Time:您可以使用DateTime.TryParseDateTimeStyles.AdjustToUniversal以获得完全相同的时间,或使用DateTimeStyles.AssumeUniversal来调整到本地时间:

 bool result = DateTime.TryParse("2018-12-30T18:30:00.000Z", CultureInfo.CurrentCulture, 
                                 DateTimeStyles.AdjustToUniversal, out DateTime dt);

=> 12/30/2018 18:30:00 or 30/12/2018 18:30:00 (...)

 bool result = DateTime.TryParse("2018-12-30T18:30:00.000Z", CultureInfo.CurrentCulture, 
                                 DateTimeStyles.AssumeUniversal, out DateTime dt);

=> 30/12/2018 [Adjusted to your Local time] (...)

This would give you a string in desired format这将为您提供所需格式的字符串

(DateTime.Parse("2018-12-30T18:30:00.000Z" )).ToString("dd.MM.yyyy HH:mm:ss.fff z"); (DateTime.Parse("2018-12-30T18:30:00.000Z")).ToString("dd.MM.yyyy HH:mm:ss.fff z");

string dateString="2018-12-30T18:30:00.000z";
Console.WriteLine(DateTimeOffset.Parse(dateString));

See the Output of in the image below it's working请参阅下图中的输出它正在工作在此处输入图片说明

暂无
暂无

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

相关问题 将不带分隔符的纯字符串转换为格式为“dd.MM.yyyy HH:mm:ss.fff z”的日期 - Convert plain string without seperator to date with format 'dd.MM.yyyy HH:mm:ss.fff z' 将dd / MM / yyyy hh:mm:ss.fff从String转换为C#中的DateTime - Convert dd/MM/yyyy hh:mm:ss.fff from String to DateTime in C# 字符串到日期时间“hh:mm:ss”或“dd.mm.yyyy hh:mm:ss”格式 - string to date time “hh:mm:ss” or “dd.mm.yyyy hh:mm:ss” format 尝试以(yyyy-mm-dd hh:mm:ss)日期格式转换字符串格式(dd-mm-yyyy hh:mm:ss) - Trying to convert string format (dd-mm-yyyy hh:mm:ss) in (yyyy-mm-dd hh:mm:ss) date format 在C#中将hh:mm:ss:fff转换为ss.fff - convert hh:mm:ss:fff to ss.fff in C# DateTime转换和解析DateTime.Now.ToString(“MM / dd / yyyy hh:mm:ss.fff”) - DateTime Conversion and Parsing DateTime.Now.ToString(“MM/dd/yyyy hh:mm:ss.fff”) 从格式 dd.MM.yyyy hh:mm:ss 将字符串解析为 .netcore 中的日期时间 - Parsing a string to datetime in .netcore from format dd.MM.yyyy hh:mm:ss 如何将字符串“dd.MM.yyyy HH:mm:ss.mmm”转换为 c# 中的日期时间 - how to convert string “dd.MM.yyyy HH:mm:ss.mmm” to the datetime in c# 将HH:mm:ss,dd-MM-yyyy从字符串转换为日期格式 - Convert HH:mm:ss,dd-MM-yyyy from string to date format DateTime.ToString(“MM/dd/yyyy HH:mm:ss.fff”) 导致类似“09/14/2013 07.20.31.371” - DateTime.ToString(“MM/dd/yyyy HH:mm:ss.fff”) resulted in something like “09/14/2013 07.20.31.371”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM