如何将此 UTC 日期时间字符串转换为 DateTime 对象 c#
How to convert this UTC datetime string to DateTime object c#
我一直在尝试将此字符串转换为 C# 中的 DateTime 对象
2019-09-23T08:34:00UTC+1
我试过使用 DateTime.Parse 但它抛出了
的异常
"String was not recognized as a valid DateTime."
使用TryParseExact
将字符串转换为日期时间。这是将给定格式转换为 datetime
的示例代码
private static DateTime ParseDate(string providedDate) {
DateTime validDate;
string[] formats = {
"yyyy-MM-ddTHH:mm:ss"
};
var dateFormatIsValid = DateTime.TryParseExact(
providedDate, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out validDate);
return dateFormatIsValid ? validDate: DateTime.MinValue;
}
然后,使用这个函数来转换字符串。我正在将 UTC+1 替换为空字符串
static void Main(string[] args) {
string strdatetime = "2019-09-23T08:34:00UTC+1";
DateTime dateTime = ParseDate(strdatetime.Replace("UTC+1", ""));
Console.WriteLine(dateTime);
}
对不起,你看起来像是 garbage in, garbage out 的受害者。
这是一种 不寻常的 格式,这就是为什么在我为您建议解决方案之前,我首先要说的是 "Fix your input first if you can".
假设您无法修复您的输入,那么您需要考虑一些事情;
- 首先,如果您的字符串包含
UTC
and/or GMT
等部分,则没有自定义日期和时间格式说明符来解析它们。这就是为什么你需要将它们转义为 string literal. See this question for more details.
- 其次,您的
+1
部分看起来像 UTC Offset value. The "z"
custom format specifier is what you need for parse it but be careful, this format specifier is not recommended for use with DateTime
values since it doesn't reflect the value of an instance's Kind
property。
作为 DateTime
的解决方案,您可以按照我的建议进行解析;
var s = "2019-09-23T08:34:00UTC+1";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ss'UTC'z", CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal, out dt))
{
Console.WriteLine(dt);
}
它给你 2019-09-23 07:34:00
作为 DateTime
并且它有 Utc
作为 Kind
属性.
作为 DateTimeOffset
的解决方案 - 由于您的字符串具有 UTC 偏移值,因此您应该考虑使用此值而不是 Datetime
进行解析
-, 作为 Matt , you can use it's .DateTime
property 来获取它的数据;
var s = "2019-09-23T08:34:00UTC+1";
DateTimeOffset dto;
if(DateTimeOffset.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ss'UTC'z", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dto))
{
Console.WriteLine(dto.DateTime);
}
这给你 相同 结果 DateTime
但 Unspecified
作为 .Kind
属性.
但是,我再次强烈建议您先修正您的输入。
我一直在尝试将此字符串转换为 C# 中的 DateTime 对象
2019-09-23T08:34:00UTC+1
我试过使用 DateTime.Parse 但它抛出了
的异常"String was not recognized as a valid DateTime."
使用TryParseExact
将字符串转换为日期时间。这是将给定格式转换为 datetime
private static DateTime ParseDate(string providedDate) {
DateTime validDate;
string[] formats = {
"yyyy-MM-ddTHH:mm:ss"
};
var dateFormatIsValid = DateTime.TryParseExact(
providedDate, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out validDate);
return dateFormatIsValid ? validDate: DateTime.MinValue;
}
然后,使用这个函数来转换字符串。我正在将 UTC+1 替换为空字符串
static void Main(string[] args) {
string strdatetime = "2019-09-23T08:34:00UTC+1";
DateTime dateTime = ParseDate(strdatetime.Replace("UTC+1", ""));
Console.WriteLine(dateTime);
}
对不起,你看起来像是 garbage in, garbage out 的受害者。
这是一种 不寻常的 格式,这就是为什么在我为您建议解决方案之前,我首先要说的是 "Fix your input first if you can".
假设您无法修复您的输入,那么您需要考虑一些事情;
- 首先,如果您的字符串包含
UTC
and/orGMT
等部分,则没有自定义日期和时间格式说明符来解析它们。这就是为什么你需要将它们转义为 string literal. See this question for more details. - 其次,您的
+1
部分看起来像 UTC Offset value. The"z"
custom format specifier is what you need for parse it but be careful, this format specifier is not recommended for use withDateTime
values since it doesn't reflect the value of an instance'sKind
property。
作为 DateTime
的解决方案,您可以按照我的建议进行解析;
var s = "2019-09-23T08:34:00UTC+1";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ss'UTC'z", CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal, out dt))
{
Console.WriteLine(dt);
}
它给你 2019-09-23 07:34:00
作为 DateTime
并且它有 Utc
作为 Kind
属性.
作为 DateTimeOffset
的解决方案 - 由于您的字符串具有 UTC 偏移值,因此您应该考虑使用此值而不是 Datetime
进行解析
-, 作为 Matt .DateTime
property 来获取它的数据;
var s = "2019-09-23T08:34:00UTC+1";
DateTimeOffset dto;
if(DateTimeOffset.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ss'UTC'z", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dto))
{
Console.WriteLine(dto.DateTime);
}
这给你 相同 结果 DateTime
但 Unspecified
作为 .Kind
属性.
但是,我再次强烈建议您先修正您的输入。