Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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# 获取时区的日期时间偏移量_C#_Azure - Fatal编程技术网

C# 获取时区的日期时间偏移量

C# 获取时区的日期时间偏移量,c#,azure,C#,Azure,我有一个代码,我需要找到中欧时区与UTC的当前偏移量 我的代码部署在azure应用程序服务中 var offset = DateTimeOffset.Now.Offset.Hours + ":" + DateTimeOffset.Now.Offset.Minutes; 上面代码的问题是,它依赖于服务器时间,即使我的应用程序服务位于西欧,Azure应用程序服务的时区始终是UTC 一个解决方案是将Azure应用程序服务时区更改为所需的时区,它会起作用,但我也在考虑使用代

我有一个代码,我需要找到中欧时区与UTC的当前偏移量

我的代码部署在azure应用程序服务中

var offset = DateTimeOffset.Now.Offset.Hours + ":" + 
             DateTimeOffset.Now.Offset.Minutes;
上面代码的问题是,它依赖于服务器时间,即使我的应用程序服务位于西欧,Azure应用程序服务的时区始终是UTC

一个解决方案是将Azure应用程序服务时区更改为所需的时区,它会起作用,但我也在考虑使用代码获取偏移量

请记住,我不能使用系统日期时间获取偏移量,因为它始终是UTC

无论系统日期时间如何,我都可以获取当前中欧日期时间偏移量吗?

您可以使用:


你可以这样做

TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");

DateTimeOffset offset = TimeZoneInfo.ConvertTime(DateTime.Now, cet);
DateTime nowCet = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, 
    "Central European Standard Time");
如上所述

如果您不确定某个
时区ID
,可以使用来查找它

如前所述,另一种选择是这样做

TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");

DateTimeOffset offset = TimeZoneInfo.ConvertTime(DateTime.Now, cet);
DateTime nowCet = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, 
    "Central European Standard Time");
这样做的好处是,您可以轻松计算两个时区之间的差异,例如

DateTime newYork = new DateTime(2017, 10, 04, 12, 23, 00);
DateTime berlin = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(newYork, 
    "Eastern Standard Time", "Central European Standard Time");

TimeSpan diff = berlin - newYork;

TimeZoneInfo没有ConvertTo方法。但我想时间是个选择,我会的try@MandarJogalekar更正了。检查post.One-liner:
DateTimeOffset nowCet=TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.Now,“中欧标准时间”)。这是否考虑到日光节约?@MandarJogalekar我不确定你的意思?我的意思是,当时钟因日光节约而改变时,该方法是否会根据当时的设置返回正确的时间?@MandarJogalekar-是。我想如果你在时钟变化的时候尝试这样做可能会有问题,但我认为这可能是不太可能的,所以我不会担心。