0

I'm using a javascript snippet to get the timezone name of the user. Here is the snippet

function getTimezoneName() {
    const today = new Date();
    const short = today.toLocaleDateString(undefined);
    const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

    // Trying to remove date from the string in a locale-agnostic way
    const shortIndex = full.indexOf(short);
    if (shortIndex >= 0) {
        const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);

        // by this time `trimmed` should be the timezone's name with some punctuation -
        // trim it from both sides
        return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

    } else {
        // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
        return full;
    }
}

console.log(getTimezoneName());

After getting the timezone of the user I am saving it to the database for timezone conversion purposes. This solution was working fine before the "Central Daylight Time" timezone Id. I was taking the user timezone from the database and converting the UTC time to user's timezone using FindSystemTimeZoneById.

Here is my extension method:

public static DateTime ToClientTime(this DateTime dt)
    {
        var tz = TimeZoneInfo.FindSystemTimeZoneById(HttpContext.Current.User.GetTimeZone());
        var tzTime = TimeZoneInfo.ConvertTimeFromUtc(dt, tz);
        return tzTime;
    }

Now, the issue is javascript snippet is showing me the timezone id "Central Daylight Time" but in the timezones provided list there is no key with this name.

Can anyone provide me the solution for this issue.

Abdul Rauf
  • 13
  • 1
  • 6
  • Central Daylight Time is what Central Standard Time is called during daylight savings. Similar for other zones. You might need to find/replace Daylight=>Standard – pinkfloydx33 Mar 21 '21 at 21:47
  • Is this a proper solution for this issue? – Abdul Rauf Mar 22 '21 at 18:55
  • Because there are 140 timezone keys and there are 130 with `Standard`. – Abdul Rauf Mar 22 '21 at 18:59
  • Maybe related to Windows/IANA(Linux) naming difference, see here: https://stackoverflow.com/questions/17348807/how-to-translate-between-windows-and-iana-time-zones – bruegth Mar 22 '21 at 20:13

0 Answers0