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>

Dani AI

Generated

Common causes why the displayed match time never changes: the stored timestamp isn't a true UTC instant (DateTime.Kind may be Unspecified or Local), the session entry isn't the timezone you think (serialisation or wrong ID), or the browser-sent zone is an IANA name while the server expects a Windows ID. was right to ask for specifics; the first useful diagnostics are a couple of logged values: the raw match timestamp, its Kind, and the session timezone identifier.

Practical checklist and patterns (safe, server-side approach):

  • Log: matchDate, matchDate.Kind and Session["timeZoneId"] (store an ID string, not a TimeZoneInfo object).
  • Capture the user's zone client-side with the standard API:
    var tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
    // send tz to server once per session
  • On the server, get a TimeZoneInfo from a known ID and convert from a UTC instant using DateTimeOffset (safer than raw DateTime):
    var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneIdFromSession);
    DateTimeOffset utcInstant = DateTime.SpecifyKind(matchDate, DateTimeKind.Utc);
    DateTimeOffset local = TimeZoneInfo.ConvertTime(utcInstant, tz);
  • Beware IANA vs Windows IDs (mapping or a library like NodaTime helps), and test around DST transitions and ambiguous local times.

Echoing and : treat timezones as a domain problem, log explicit values, and prefer proven libraries when mapping/complex rules are needed.

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.