c# - 无法在 C# 中将字符串解析为精确的日期时间

标签 c# windows-phone-7 datetime

我有字符串格式的日期时间。 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/

相关文章:

c# - 如何在 Windows 窗体/WPF 中为数据库 View (带连接)创建编辑器?

c# - 在 asp.net 中写入 csv 文件

c# - 使用 DropNet API 从我的 Dropbox 帐户获取所有文件的列表

python - 根据时间条件获取每个组的最大值

c# - 哪种方式是从 Sbyte[] 转换为 Byte[] 的最佳方式

c# - 使用隔离存储时未处理 SecurityException

c# - 获取 "Operation not permitted on IsolatedStorageFileStream."错误

javascript - yyyy-mm-dd 格式的两天之间的差异(以天为单位)?

java - 如何从 Java 8 中的 LocalDateTime 获取毫秒数

c# - 如何使用 iTextSharp 将段落放置在特定位置