【问题标题】:Convert UTC TimeSpan to IST TimeSpan in C#在 C# 中将 UTC 时间跨度转换为 IST 时间跨度
【发布时间】: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 的时间。这怎么可能?

标签: c# timezone


【解决方案1】:

试试这个。

DateTime utcdate = DateTime.ParseExact("6/15/2014 12:00:00 AM", "M/dd/yyyy 
                                       h:mm:ss tt",CultureInfo.InvariantCulture);
var istdate = TimeZoneInfo.ConvertTimeFromUtc(utcdate,
TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));

【讨论】:

  • 这与OP提到的TimeSpans有什么关系?
猜你喜欢
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2019-10-14
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多