如何表示当前英国时间? [英] How to represent the current UK time?

查看:365
本文介绍了如何表示当前英国时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的一个问题,而我的服务器和客户端之间的转换日期,其中两个在德国运行。在客户端计算机的区域设置可以设置无论对英国还是Germany.I收到来自这CET格式与服务器的日期,我要代表这个时候作为UI英国时间。例如一时间从服务器收到就好说了,01/07/2010 01:00:00应该在UI上表示为01/07/2010 00:00:00。我写了一个转换器,用于此目的,但是同时运行它'我得到2 hours.Below的时间差是代码,请你能帮忙吗?



 公共类LocalToGmtConverter:IDateConverter 
{
私人只读的TimeZoneInfo的TimeZoneInfo;

公共LocalToGmtConverter()
:这(TimeZoneInfo.Local)
{

}
公共LocalToGmtConverter(的TimeZoneInfo的TimeZoneInfo)
{
this.timeZoneInfo =的TimeZoneInfo;
}

公众的DateTime转换(日期时间LOCALDATE)
{
VAR utcKind = DateTime.SpecifyKind(LOCALDATE,DateTimeKind.Utc);
返回utcKind;
}

公众的DateTime ConvertBack(对象fromServer)
{
的DateTime serverDate =(DateTime的)fromServer;

VAR UTCOFFSET = timeZoneInfo.GetUtcOffset(serverDate);

VAR uiTime = serverDate- UTCOFFSET;

返回uiTime;

}
}


解决方案

我觉得你转换为UTC(而不是英国)的时间。由于仍有中欧夏季时间(事件如果温度正好相反),差异为+2小时,直到十月,31日。



如果您知道再(在夏季和CET即CEST以BST为GMT在冬季)由德国转换为英国,为什么你不只是减去1时间?



如果你想要的英国时区信息,您可以用它构建

  VAR britishZone = TimeZoneInfo.FindSystemTimeZoneById(GMT标准时间); 



然后,你可以使用



<$转换日期p $ p> VAR newDate = TimeZoneInfo.ConvertTime(serverDate,TimeZoneInfo.Local,britishZone);


I'm facing an issue while converting dates between my server and client where both is running in Germany. The Regional settings on the client machines could be set to both UK or Germany.I recieve a date from the server which is CET format, and I need to represent this time on UI as UK time. For example a time recieved from server like say, 01/07/2010 01:00:00 should be represented on the UI as 01/07/2010 00:00:00. I have written a converter for this purpose, however while running it 'am getting a time difference of 2 hours.Below is the code, please can you help?

public class LocalToGmtConverter : IDateConverter
{
    private readonly TimeZoneInfo timeZoneInfo;

    public LocalToGmtConverter()
        : this(TimeZoneInfo.Local)
    {

    }
    public LocalToGmtConverter(TimeZoneInfo timeZoneInfo)
    {
        this.timeZoneInfo = timeZoneInfo;
    }

    public DateTime Convert(DateTime localDate)
    {
        var utcKind = DateTime.SpecifyKind(localDate, DateTimeKind.Utc);
        return utcKind;
    }

    public DateTime ConvertBack(object fromServer)
    {
        DateTime serverDate = (DateTime)fromServer;

        var utcOffset = timeZoneInfo.GetUtcOffset(serverDate);

        var uiTime = serverDate- utcOffset;

        return uiTime;

    }
}

解决方案

I think you're converting to UTC (instead of UK) time. Since there is still summer time in Central Europe (event if the temperatures say otherwise), the difference is +2 hours until October, 31st.

If you know that you're converting from Germany to UK (i.e. CEST to BST in summer and CET to GMT in winter), why you don't just subtract 1 hour?

If you want the time zone information for UK, you can construct it using

var britishZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");

Then you could convert the date using

var newDate = TimeZoneInfo.ConvertTime(serverDate, TimeZoneInfo.Local, britishZone);

这篇关于如何表示当前英国时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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