【发布时间】:2016-09-28 13:24:37
【问题描述】:
我在 UTC 区域的服务器上的 TimeSpan 中存储了一天中的各种时间。现在我想在 IST 区域中检索该记录。在我的服务器上,数据采用 HH:mm:ss 格式,我希望以与 IST 区域相同的格式取回它。我可以使用以下代码将 DateTime 转换为 TimeSpan,但我无法从 TimeSpan 转换为 TimeSpan。
DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo istZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime istTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, istZone);
return string.Format("The date and time are {0}.", istTime.TimeOfDay.ToString().Substring(0, 6));
【问题讨论】:
-
TimeSpan测量时间跨度(持续时间),不是一天中的某个时间。您提供的代码中的TimeSpan在哪里? -
Timespan 是两个 DateTime 对象之间的增量,并且没有 TimeZone,因为测量值是差分的。
-
哦。首先很抱歉耽搁了。我需要编辑我的问题。我的意思是说让用户在一天中的任何特定时间在 UTC 中注册。我只想在 HH:mm:ss 的基础上获得 IST 的时间。这怎么可能?