【问题标题】:How to display date time field based on local time zone using Noda time c#如何使用 Noda 时间 c# 根据本地时区显示日期时间字段
【发布时间】:2014-06-09 07:38:38
【问题描述】:

我尝试过使用下面的代码,但是没有用

Instant instant = Instant.FromDateTimeUtc(DateTime.Now);
IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
var usersTimezone = timeZoneProvider[Request.Cookies["ahcs"]["timeZone"].ToString()];
var usersZonedDateTime = instant.InZone(usersTimezone);

【问题讨论】:

  • 我想,如果我没记错的话,默认是本地时区。
  • 我有两个下拉列表,一个包含国家/地区列表,另一个包含与泰国国家相关的时区。根据国家和时区,用户选择需要转换为该时区的日期时间字段
  • 这从表面上看是合理的(尽管我不确定为什么您需要在 cookie 值上调用 .ToString()),假设您的 cookie 以某种方式填充了 TZDB 时区。什么不起作用:你得到一个错误,还是错误的结果?请在问题中添加更多信息。
  • @Yogesh:不,因为没有必要的相关性。例如,我可能是在美国的法国人。此外,Instant.FromDateTimeUtc(DateTime.Now) 已损坏 - 要么使用 DateTime.UtcNow,要么(最好)使用 IClock,例如通过SystemClock.Instance。这就是它的用途。
  • @Yogesh - “它没用”不是很有帮助。 Please read this。此外,我们如何知道该 cookie 返回的值是什么?你应该提供一个例子。请相应地编辑您的问题。谢谢。

标签: c# nodatime


【解决方案1】:

试试这个

TimeZoneInfo timeZonetoConvert = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId2);
    DateTime newTime = new DateTime();

    TimeZoneInfo timeZonefromConvert = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId2);
    newTime = TimeZoneInfo.ConvertTime(DateTimeToConvert, timeZonefromConvert, timeZonetoConvert );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多