Get UTC offset in WP7

北城以北 提交于 2019-12-24 06:01:26

问题


Does anyone know the best way to get the UTC offset in WP7? Apparently the TimeZone api hasn't been implemented there.

Thanks


回答1:


Just use DateTimeOffset.Now.Offset... that is why DateTimeOffset was created!




回答2:


If you just want to convert to local time, use DateTime.ToLocalTime(). If you want the offset, I think you can subtract the original value. Documentation.




回答3:


I recently did something like the below; this was done quickly but it works. In my case I always wanted time as of eastern time zone since that is where the event my app was for. Stored as UTC and calculated offset.

    private DateTime _startTimeUtc;
    private DateTime _startTime;

    public DateTime StartTime
    {
        get { return _startTime; }
        set
        {
            _startTimeUtc = value.ToUniversalTime();
            _startTime = _startTimeUtc.Subtract(EasternTimeUtcOffSet);
        }
    }

    public DateTime StartTimeUtc 
    {
        get { return _startTimeUtc; }

        set
        {
            _startTimeUtc = value;
            _startTime = _startTimeUtc.Subtract(EasternTimeUtcOffSet);
        }
    }

    private static TimeSpan EasternTimeUtcOffSet
    {
        get { return TimeSpan.FromHours(4); }
    }



回答4:


  private int TimeZoneOffset()
  {
      DateTime dt = DateTime.Now;
      return dt.Subtract(dt.ToUniversalTime()).Hours;
  }


来源:https://stackoverflow.com/questions/6182017/get-utc-offset-in-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!