我有字符串格式的日期时间。
2013-03-05T08:28:18+0000
。
我需要如下输出:- Mar 5, 2013 8:28:18 PM
我正在尝试解析为日期时间格式。
string givenDate="2013-03-05T08:28:18+0000";
DateTime parsedDate= DateTime.Parse(givenDate);
string output= parsedDate.ToString("MMM d, yyyy hh:mm:ss tt");
Console.WriteLine("OutPut is==>"+output);
但我的出局时间是:-2013 年 3 月 5 日 01:58:18 PM
时间正在发生变化。我的预期输出是 -:-Mar 5, 2013 8:28:18 PM
我也这样尝试过。
parsedDate = DateTime.ParseExact("2013-03-05T08:28:18+0000", "MMM d, yyyy hh:mm:ss tt", new CultureInfo("tr-TR"));
但这里我收到错误:- 值不在预期范围内。
我也尝试过这样的:-
parsedDate = DateTime.ParseExact("2013-03-05T08:28:18+0000", "MMM d, yyyy hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
还有这个
parsedDate = DateTime.ParseExact("2013-03-05T08:28:18+0000", "MMM d, yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
两者都显示此错误:- FormatException
请帮助将日期时间转换为我需要的。
最佳答案
试试这个方法
string givenDate = ("2013-03-05T08:28:18+0000");
DateTime d = DateTime.Parse(givenDate, System.Globalization.CultureInfo.InvariantCulture);
string ouputDate = d.ToUniversalTime().ToString("MMM d, yyyy h:m:s tt", System.Globalization.CultureInfo.InvariantCulture);
关于c# - 无法在 C# 中将字符串解析为精确的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24094778/