我怎样才能在asp.net中获得CST(中央标准时间)? [英] How can i get CST(Central Standard Time) in asp.net?

查看:95
本文介绍了我怎样才能在asp.net中获得CST(中央标准时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



如何在asp.net中获得CST?我目前正在我的网站上显示一个计时器并显示当前的服务器时间。我需要在计时器上显示CST。



有什么建议吗?



提前致谢

解决方案

如果使用3.5或更大的框架,则可以使用TimeZoneInfo类

 TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById(  中央标准时间); 
DateTime newDT = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,targetZone);


小心:ConvertTimeFromUtc返回当前日期/该时区的时间(如果DST在该区域中处于活动状态,则自动调整夏令时(DST))。

 TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById(' 中央标准时间'); 
DateTime newDT = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,targetZone);



因此,如果DST当前有效,结果将是DST - 而不是标准时间。



由于中央标准时间是格林威治标准时间-6小时,这是获得CST最简单/最快捷的方式(一年中任何时候)是:

  var  cst = DateTime.UtcNow.AddHours(-6); 


HI,

How can i get CST in asp.net? I am currently displaying a timer on my website and showing current server time. I need to show CST on timer.

Any suggestions??

Thanks in advance

解决方案

If using the 3.5 framework or greater you can use the TimeZoneInfo class

TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime newDT = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, targetZone);


Beware: ConvertTimeFromUtc returns the current date/time in that time zone (automatically adjusting for daylight savings time (DST) if DST is active in that zone).

TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById('Central Standard Time');
DateTime newDT = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, targetZone);


So if DST is currently active, the result will be in DST - not standard time.

Since Central Standard Time is GMT -6 hours, the easiest/fastest way to get CST (at any time of the year) is:

var cst = DateTime.UtcNow.AddHours(-6);


这篇关于我怎样才能在asp.net中获得CST(中央标准时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆