Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent the current UK time?

Tags:

c#

datetime

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;

    }
}
like image 419
Mike Avatar asked Oct 27 '10 15:10

Mike


People also ask

How do you indicate UK time?

Date and time notation in the United Kingdom records the date using the day–month–year format (31 December 1999, 31/12/99 or 31/12/1999). The ISO 8601 format (1999-12-31) is increasingly used for all-numeric dates. The time can be written using either the 24-hour clock (23:59) or the 12-hour clock (11:59 pm).

Is UK on GMT or BST now?

GMT is the standard time zone in Ireland and the United Kingdom, including England, Wales, Scotland, and Northern Ireland.

What is the symbol for UK time zone?

GMT stands for Greenwich Mean Time, the local clock time at Greenwich. From 1884 until 1972, GMT was the international standard of civil time.

Does UK use PT or ET?

The time zone for the capital London is used here. When the time was 09:00AM on Tuesday, February 16 in Pacific Time, it was 12:00PM in Eastern Time, and 05:00PM in Greenwich Mean Time and United Kingdom. Pacific Time is 3 hours behind Eastern Time, and 8 hours behind Greenwich Mean Time and United Kingdom.


2 Answers

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);
like image 73
MartinStettner Avatar answered Nov 14 '22 00:11

MartinStettner


This is what I do:

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

    DateTime dt = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified);

    DateTime DateTimeInBritishLocal = TimeZoneInfo.ConvertTime(dt, TimeZoneInfo.Utc, BritishZone);

I needed to add the SpecifyKind part or the ConvertTime throws an exception

like image 22
noelicus Avatar answered Nov 13 '22 22:11

noelicus