简体   繁体   English

无法完成转换,因为提供的 DateTime 没有正确设置 Kind 属性

[英]The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly

I am trying convert UK time into UTC in C#我正在尝试在 C# 中将英国时间转换为 UTC

DateTime utc = DateTime.UtcNow;
var ukTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTime ukTime = TimeZoneInfo.ConvertTime(utc,ukTimeZone, TimeZoneInfo.Local );

Console.WriteLine(ukTime);

But I got this error.但我得到了这个错误。

Unhandled exception.未处理的异常。 System.ArgumentException: The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly. System.ArgumentException:转换无法完成,因为提供的 DateTime 没有正确设置 Kind 属性。 For example, when the Kind property is DateTimeKind.Local, the source time zone must be TimeZoneInfo.Local.例如,当 Kind 属性为 DateTimeKind.Local 时,源时区必须为 TimeZoneInfo.Local。 (Parameter 'sourceTimeZone') at System.TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone, TimeZoneInfoOptions flags, CachedData cachedData) at System.TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone) (参数'sourceTimeZone')在 System.TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone, TimeZoneInfoOptions flags, CachedData cachedData) 在 System.TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)

But if I run the below code但是如果我运行下面的代码

DateTime time = new DateTime(2022, 11, 09, 23, 47, 00);
var ukTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTime ukTime = TimeZoneInfo.ConvertTime(time,ukTimeZone, TimeZoneInfo.Local );
Console.WriteLine(ukTime);

I got the expected result: 11/09/2022 23:47:00我得到了预期的结果: 11/09/2022 23:47:00

Why does this error occur?为什么会出现这个错误? What is the difference between time and utc ? timeutc和有什么不一样?

The DateTimeKind associated with a DateTime is determined by how you construct it:DateTimeKind关联的DateTime取决于您如何构造它:

DateTime.UtcNow.Kind == DateTimeKind.Utc
DateTime.Now.Kind == DateTimeKind.Local
new DateTime(2022, 11, 9, 23, 47, 0) == DateTimeKind.Unspecified

The docs for the TimeZoneInfo.ConvertTime method you are calling describes the behavior you are observing:您正在调用的TimeZoneInfo.ConvertTime方法的文档描述了您正在观察的行为:

Exceptions例外

ArgumentException

The Kind property of the dateTime parameter is Local , but the sourceTimeZone parameter does not equal Local . dateTime参数的Kind属性是Local ,但sourceTimeZone参数不等于Local

-or- -或者-

The Kind property of the dateTime parameter is Utc , but the sourceTimeZone parameter does not equal Utc . dateTime参数的Kind属性是Utc ,但sourceTimeZone参数不等于Utc

-or- -或者-

The dateTime parameter is an invalid time (that is, it represents a time that does not exist because of a time zone's adjustment rules). dateTime参数为无效时间(即,由于时区的调整规则,它表示不存在的时间)。

Your first example demonstrates the second reason described for the exception.您的第一个示例演示了为异常描述的第二个原因。 That is:那是:

  • The Kind property of the input value is DateTimeKind.Utc输入值的Kind属性是DateTimeKind.Utc
  • The source time zone you are passing is "GMT Standard Time" , not "UTC"您传递的源时区是"GMT Standard Time" ,而不是"UTC"

These time zones are not the same.这些时区不相同。

  • "GMT Standard Time" corresponds to "(UTC+00:00) Dublin, Edinburgh, Lisbon, London" "GMT Standard Time"对应于"(UTC+00:00) Dublin, Edinburgh, Lisbon, London"
  • "UTC" corresponds to "(UTC) Coordinated Universal Time" "UTC"对应于"(UTC) Coordinated Universal Time"

The primary difference being that London uses UTC+1 during the summer months when British Summer Time (a daylight saving time) is in effect.主要区别在于伦敦在英国夏令时(夏令时)生效的夏季月份使用 UTC+1。 Reference: https://www.timeanddate.com/time/zone/uk/london参考: https://www.timeanddate.com/time/zone/uk/london

Conversely, UTC is not necessarily the time in the UK.相反,UTC 不一定是英国的时间。 It's a time standard that applies universally to the entire planet.这是一个普遍适用于整个星球的时间标准。 It never observes daylight saving time.它从不遵守夏令时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于类型不匹配,无法完成DateTime转换 - DateTime Conversion Could not be completed because of Kind Mismatch DateTime.kind 属性 - DateTime.kind property 由于用户没有一个或多个工作空间所需的权限,因此操作无法完成 - The operation could not be completed because the user does not have one or more required permissions for workspace SonarQube 分析无法完成,因为找不到分析配置文件 - SonarQube analysis could not be completed because the analysis configuration file could not be found 如何从具有Datetime属性的对象的IEnumerble列表中的Ajax调用正确序列化Datetime - How can I correctly serialize Datetime from an Ajax call inside an IEnumerble list of objects that have a Datetime property 让C#正确设置解析后的ISO 8601字符串的Kind属性 - Getting C# to correctly set the Kind property for a parsed ISO 8601 string 如何设置 DateTime 值的时区(或种类)? - How to set a time zone (or a Kind) of a DateTime value? protobuf-net 没有正确反序列化 DateTime.Kind - protobuf-net does not deserialize DateTime.Kind correctly 为什么System.DateTime结构具有布局类型Auto? - Why does the System.DateTime struct have layout kind Auto? 设置可空日期时间的属性值 - Set property value of nullable datetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM