问题
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