如何将包含 GMT 的字符串解析为 DateTime?
How to parse string which contains GMT to DateTime?
如何转换这个字符串:
string aa ="Thu Jul 02 2015 00:00:00 GMT+0100 (GMT Standard Time)";
转换为日期时间。
我尝试使用 Convert.ToDateTime(aa);
但没有成功
谢谢
编辑:错误消息 - 字符串未被识别为有效的日期时间
使用DateTime.Parse方法:
using System;
public class Example
{
public static void Main()
{
string[] dateStrings = {"2008-05-01T07:34:42-5:00",
"2008-05-01 7:34:42Z",
"Thu, 01 May 2008 07:34:42 GMT"};
foreach (string dateString in dateStrings)
{
DateTime convertedDate = DateTime.Parse(dateString);
Console.WriteLine("Converted {0} to {1} time {2}",
dateString,
convertedDate.Kind.ToString(),
convertedDate);
}
}
}
// These calls to the DateTime.Parse method display the following output:
// Converted 2008-05-01T07:34:42-5:00 to Local time 5/1/2008 5:34:42 AM
// Converted 2008-05-01 7:34:42Z to Local time 5/1/2008 12:34:42 AM
// Converted Thu, 01 May 2008 07:34:42 GMT to Local time 5/1/2008 12:34:42 AM
您可以将 DateTime.TryParseExact
与正确的格式字符串一起使用:
string dtString = "Thu Jul 02 2015 00:00:00 GMT+0100";
string format = "ddd MMM dd yyyy HH:mm:ss 'GMT'K";
DateTime date;
bool validFormat = DateTime.TryParseExact(dtString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
Console.Write(validFormat ? date.ToString() : "Not a valid format");
如果字符串末尾包含 (GMT Standard Time)
,您可以先将其删除:
dtString = dtString.Replace("(GMT Standard Time)", "").Trim();
或使用此格式模式:
string format = "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'";
因为你有 UTC offset in your string, I would prefer to parse DateTimeOffset
而不是 DateTime
。并且没有办法在不转义的情况下解析您的 GMT
和 (GMT Standard Time)
部分。
DateTime
和DateTimeOffset
都是时区意识。对于这种情况,DateTimeOffset
比 DateTime
好一点。它具有 UTC 偏移量,但这并不能保证时区信息,因为不同的时区可以具有相同的偏移量值。
即使是,时区缩写也没有标准化。例如,CST
有多种含义。
string s = "Thu Jul 02 2015 00:00:00 GMT+01:00 (GMT Standard Time)";
DateTimeOffset dto;
if (DateTimeOffset.TryParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dto))
{
Console.WriteLine(dto);
}
现在,您有一个 DateTimeOffset
作为 {02.07.2015 00:00:00 +01:00}
如何转换这个字符串:
string aa ="Thu Jul 02 2015 00:00:00 GMT+0100 (GMT Standard Time)";
转换为日期时间。
我尝试使用 Convert.ToDateTime(aa);
但没有成功
谢谢
编辑:错误消息 - 字符串未被识别为有效的日期时间
使用DateTime.Parse方法:
using System;
public class Example
{
public static void Main()
{
string[] dateStrings = {"2008-05-01T07:34:42-5:00",
"2008-05-01 7:34:42Z",
"Thu, 01 May 2008 07:34:42 GMT"};
foreach (string dateString in dateStrings)
{
DateTime convertedDate = DateTime.Parse(dateString);
Console.WriteLine("Converted {0} to {1} time {2}",
dateString,
convertedDate.Kind.ToString(),
convertedDate);
}
}
}
// These calls to the DateTime.Parse method display the following output:
// Converted 2008-05-01T07:34:42-5:00 to Local time 5/1/2008 5:34:42 AM
// Converted 2008-05-01 7:34:42Z to Local time 5/1/2008 12:34:42 AM
// Converted Thu, 01 May 2008 07:34:42 GMT to Local time 5/1/2008 12:34:42 AM
您可以将 DateTime.TryParseExact
与正确的格式字符串一起使用:
string dtString = "Thu Jul 02 2015 00:00:00 GMT+0100";
string format = "ddd MMM dd yyyy HH:mm:ss 'GMT'K";
DateTime date;
bool validFormat = DateTime.TryParseExact(dtString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
Console.Write(validFormat ? date.ToString() : "Not a valid format");
如果字符串末尾包含 (GMT Standard Time)
,您可以先将其删除:
dtString = dtString.Replace("(GMT Standard Time)", "").Trim();
或使用此格式模式:
string format = "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'";
因为你有 UTC offset in your string, I would prefer to parse DateTimeOffset
而不是 DateTime
。并且没有办法在不转义的情况下解析您的 GMT
和 (GMT Standard Time)
部分。
DateTime
和DateTimeOffset
都是时区意识。对于这种情况,DateTimeOffset
比 DateTime
好一点。它具有 UTC 偏移量,但这并不能保证时区信息,因为不同的时区可以具有相同的偏移量值。
即使是,时区缩写也没有标准化。例如,CST
有多种含义。
string s = "Thu Jul 02 2015 00:00:00 GMT+01:00 (GMT Standard Time)";
DateTimeOffset dto;
if (DateTimeOffset.TryParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dto))
{
Console.WriteLine(dto);
}
现在,您有一个 DateTimeOffset
作为 {02.07.2015 00:00:00 +01:00}