I am developing a website that gives statistics for soccer games,and i want to convert the schedule of the match depending on the time zone. I created this function but it seems not to be working.

public static DateTime UtcToLocalTimeZone(DateTime argDateTime,TimeZoneInfo argTimeZoneInfo)
    {
        try
        {

            return TimeZoneInfo.ConvertTimeFromUtc(argDateTime, argTimeZoneInfo);
        }
        catch (Exception)
        {

            return argDateTime;
        }

    }

And this is how i display the date and time in my view:

TimeZoneInfo timezone = (TimeZoneInfo)Session["timeZone"] ?? TimeZoneInfo.Local;
 <a style="cursor:pointer;" id="id1" versus-id="@c.MatchID" data-toggle="tooltip" data-placement="top" title="@homeMLName - @awayMLName">
                            @BetGuns.Web.Helper.UtcToLocalTimeZone(@c.MatchDate, timezone).ToString(dateFormat) <br />
                            @BetGuns.Web.Helper.UtcToLocalTimeZone(@c.MatchDate, timezone).ToString(timeFormat)
                        </a>

Recommended Answers

All 7 Replies

Not to be working...
WHAT is not working?
Error codes, your program blew up etc.
Tell us.

It does not convert the time when i change timezone.

I guess this might be helpful.

Thank you it was helpfult that article,but i think that i have a problem with how i take user timezone:
TimeZoneInfo timezone = (TimeZoneInfo)Session["timeZone"] ?? TimeZoneInfo.Local;

After having written a LOT of time conversion software over the years I can only say this. DO NOT GUESS! Study the domain and understand what is required to convert from one timezone to another. My software had to work properly in countries all over the world (China, Japan, Korea, India, Europe, USA, et al). I'm not going to try to teach you how to do this, but is isn't that hard if you make some effort!

I'll just leave this here.

commented: Sooner or later, time.... +12
commented: Indeed +15

having written a LOT of time conversion software over the years

Why? There's proven tested supported code out there that does it. Do you have some kind of magic time-lord insight that means they are all wrong and only you can get it right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.