[英]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
? time
和utc
和有什么不一样?
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 thedateTime
parameter isLocal
, but thesourceTimeZone
parameter does not equalLocal
.dateTime
参数的Kind
属性是Local
,但sourceTimeZone
参数不等于Local
。-or- -或者-
The
Kind
property of thedateTime
parameter isUtc
, but thesourceTimeZone
parameter does not equalUtc
.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:那是:
Kind
property of the input value is DateTimeKind.Utc
输入值的Kind
属性是DateTimeKind.Utc
"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.