C#:确保DateTime.Now返回GMT + 1次 [英] C#: Making sure DateTime.Now returns a GMT + 1 time

查看:243
本文介绍了C#:确保DateTime.Now返回GMT + 1次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DateTime.Now 根据今天的日期显示的东西,并在本地工作(马耳他,欧洲)当时代正确显示(显然是因为时区的),但ofcourse当我把它上传到我的托管服务器(美国), DateTime.Now 并不代表正确的时区。



因此,在我的代码,我如何转换 DateTime.Now 来正确地从一个GMT + 1时区返回时间 <? / p>

解决方案

使用在System.Core程序发现的TimeZoneInfo类;



您必须DateTimeKind设置DateTimeKind.Utc这一点。

  DateTime的数值指明MyTime =新日期时间(1990,12,02,19,31 ,30,DateTimeKind.Utc); 

的DateTime MyTimeInWesternEurope = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(数值指明MyTime,西欧标准时间);



只有当你使用的.Net 3.5,但!


I am using DateTime.Now to show something according to today's date, and when working locally (Malta, Europe) the times appear correctly (obviously because of the Time Zone) but ofcourse when I upload it to my hosting server (USA), DateTime.Now does not represent the correct time zone.

Therefore, in my code, how can I convert DateTime.Now to correctly return the time from a GMT + 1 timezone ?

解决方案

Use the TimeZoneInfo class found in System.Core;

You must set the DateTimeKind to DateTimeKind.Utc for this.

DateTime MyTime = new DateTime(1990, 12, 02, 19, 31, 30, DateTimeKind.Utc);

DateTime MyTimeInWesternEurope = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(MyTime, "W. Europe Standard Time");

Only if you're using .Net 3.5 though!

这篇关于C#:确保DateTime.Now返回GMT + 1次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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