Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net中的时区偏移_C#_Asp.net_Timezone - Fatal编程技术网

C# asp.net中的时区偏移

C# asp.net中的时区偏移,c#,asp.net,timezone,C#,Asp.net,Timezone,我有一种计算用户时间和UTC时间之间的时间偏移的方法,其工作原理如下: public int GetUserLocalTimeToUTCOffset() { DateTime TheUTC = DateTime.UtcNow; DateTime TheLocal = ConvertUTCTimeToUserTime(TheUTC); //*see below TimeSpan TheTimeOffset = TheLocal - TheUTC; return

我有一种计算用户时间和UTC时间之间的时间偏移的方法,其工作原理如下:

public int GetUserLocalTimeToUTCOffset()
{        
  DateTime TheUTC = DateTime.UtcNow;
  DateTime TheLocal = ConvertUTCTimeToUserTime(TheUTC); //*see below

  TimeSpan TheTimeOffset = TheLocal - TheUTC;

  return (int)TheTimeOffset.TotalMinutes;
}

public DateTime ConvertUTCTimeToUserTime(DateTime TheUTCTime)
{
  TimeZoneInfo TheTZ = TimeZoneInfo.FindSystemTimeZoneById(this.UserTimeZoneID);
    // try with "Eastern Standard Time" and "W. Europe Standard Time"
  DateTime UTCTime = new DateTime(TheUTCTime.Year, TheUTCTime.Month, TheUTCTime.Day,  TheUTCTime.Hour, TheUTCTime.Minute, 0, DateTimeKind.Utc);

  return TimeZoneInfo.ConvertTime(UTCTime, TheTZ);
}
如果用户在EST时区,该方法返回-300(即60*-5)。但是,如果用户在欧洲西部时间,则该方法返回59而不是60。没有那么大,但我想知道为什么它没有回到60岁,我需要做什么改变


谢谢。

属性TotalMinutes返回一个双精度值。我的猜测是,将其转换为整数会使值向下舍入

在返回值之前,请尝试将值四舍五入。我会将您的返回语句修改为“将总分钟数四舍五入”

return (int)Math.Ceiling(TheTimeOffset.TotalMinutes);

ConvertUTCTimeToUserTime中的代码可能与确定答案有关,因为它直接涉及到计算。@JeremyS:请参见编辑,我添加了另一个函数。我已对我之前的文章进行了编辑,希望该示例能够解决您的问题。