如何在给定 UTC 时间和日期的情况下创建 DateTime 对象?
How do I create a DateTime object given UTC time and date?
我已经有 UTC 格式的日期和时间字符串。我需要使用这些字符串来创建 DateTime 对象。
这是我正在使用的代码。问题是时间被转换并且我在 datetime 对象上的 UTC 时间不再正确。我正在提供 UTC 值,因此它们不会再次转换。
string format = $"{dateFormat}_{timeFormat}";
string value = $"{dateValue}_{timeValue}";
var x = DateTimeOffset.ParseExact(value, format, CultureInfo.CurrentCulture).UtcDateTime;
其中 dateFormat = "ddMMyy"
、timeFormat = "HHmmss"
、dateValue = "191194"
和 timeValue = "225446".
使用 DateTimeOffset.ParseExact
that takes a DateTimeStyles
值的重载:
var x = DateTimeOffset.ParseExact(value,
format,
CultureInfo.CurrentCulture,
DateTimeStyles.AssumeUniversal)
.UtcDateTime;
请注意,调用 UtcDateTime
不会 伤害 任何东西,但时间 已经 为 UTC 时间(这是你想要的)所以它会给你等效的 DateTime
值。您可以按照 Jon 的建议使用 DateTime.ParseExact
,它具有相同的重载。
D Stanley 的答案当然有效,但比您需要的稍微复杂一些 - 如果您 想要 结果是 DateTime
,则不需要使用DateTimeOffset
,因为 DateTime.ParseExact
也处理 DateTimeStyles.AssumeUniversal
,尽管您需要指定 AdjustToUniversal
以便 结果 为 UTC . (否则它会自动调整到当地时区 - 对我来说毫无帮助,但这是另一天的战斗。)
var x = DateTime.ParseExact(
value,
format,
CultureInfo.CurrentCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
示例代码(向我揭示了 DateTimeStyles.AdjustToUniversal
的必要性):
using System;
using System.Globalization;
class Test
{
static void Main(string[] args)
{
string text = "2015-06-10 20:52:13";
string format = "yyyy-MM-dd HH:mm:ss";
var dateTime = DateTime.ParseExact(
text,
format,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
Console.WriteLine(dateTime); // 10/06/2015 20:52:13 on my box
Console.WriteLine(dateTime.Kind); // Utc
}
}
我会小心使用 CultureInfo.CurrentCulture
,顺便说一句 - 请记住,它会影响正在使用的日历系统以及格式字符串等。
(当然,作为旁注,我建议改用我的 Noda Time 库。在这种情况下,我可能建议使用 LocalTimeFormat
解析你的时间,你的日期使用a LocalDateFormat
,然后将结果相加得到 LocalDateTime
,然后您可以使用 UTC 将其转换为 ZonedDateTime
。或者您 可以 使用您的当然,创建 ZonedDateTimePattern
或 InstantPattern
的现有方法。)
如果您的日期不是一个字符串,此解决方案也会有所帮助。
只需使用 DateTimeKind.Utc
作为 DateTime
:
的构造函数参数
new DateTime(2020, 05, 07, 18, 33, 0, DateTimeKind.Utc);
我已经有 UTC 格式的日期和时间字符串。我需要使用这些字符串来创建 DateTime 对象。
这是我正在使用的代码。问题是时间被转换并且我在 datetime 对象上的 UTC 时间不再正确。我正在提供 UTC 值,因此它们不会再次转换。
string format = $"{dateFormat}_{timeFormat}";
string value = $"{dateValue}_{timeValue}";
var x = DateTimeOffset.ParseExact(value, format, CultureInfo.CurrentCulture).UtcDateTime;
其中 dateFormat = "ddMMyy"
、timeFormat = "HHmmss"
、dateValue = "191194"
和 timeValue = "225446".
使用 DateTimeOffset.ParseExact
that takes a DateTimeStyles
值的重载:
var x = DateTimeOffset.ParseExact(value,
format,
CultureInfo.CurrentCulture,
DateTimeStyles.AssumeUniversal)
.UtcDateTime;
请注意,调用 UtcDateTime
不会 伤害 任何东西,但时间 已经 为 UTC 时间(这是你想要的)所以它会给你等效的 DateTime
值。您可以按照 Jon 的建议使用 DateTime.ParseExact
,它具有相同的重载。
D Stanley 的答案当然有效,但比您需要的稍微复杂一些 - 如果您 想要 结果是 DateTime
,则不需要使用DateTimeOffset
,因为 DateTime.ParseExact
也处理 DateTimeStyles.AssumeUniversal
,尽管您需要指定 AdjustToUniversal
以便 结果 为 UTC . (否则它会自动调整到当地时区 - 对我来说毫无帮助,但这是另一天的战斗。)
var x = DateTime.ParseExact(
value,
format,
CultureInfo.CurrentCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
示例代码(向我揭示了 DateTimeStyles.AdjustToUniversal
的必要性):
using System;
using System.Globalization;
class Test
{
static void Main(string[] args)
{
string text = "2015-06-10 20:52:13";
string format = "yyyy-MM-dd HH:mm:ss";
var dateTime = DateTime.ParseExact(
text,
format,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
Console.WriteLine(dateTime); // 10/06/2015 20:52:13 on my box
Console.WriteLine(dateTime.Kind); // Utc
}
}
我会小心使用 CultureInfo.CurrentCulture
,顺便说一句 - 请记住,它会影响正在使用的日历系统以及格式字符串等。
(当然,作为旁注,我建议改用我的 Noda Time 库。在这种情况下,我可能建议使用 LocalTimeFormat
解析你的时间,你的日期使用a LocalDateFormat
,然后将结果相加得到 LocalDateTime
,然后您可以使用 UTC 将其转换为 ZonedDateTime
。或者您 可以 使用您的当然,创建 ZonedDateTimePattern
或 InstantPattern
的现有方法。)
如果您的日期不是一个字符串,此解决方案也会有所帮助。
只需使用 DateTimeKind.Utc
作为 DateTime
:
new DateTime(2020, 05, 07, 18, 33, 0, DateTimeKind.Utc);