Search code examples
javascriptc#.netbrowsertimezone

How can use "Romance Standard Time" to set the time zone?


I have the UTC date string "2022-01-06T13:35:00Z" and the time zone string "Romance Standard Time". How can use that time zone string in JavaScript (in a browser) so that I can get the time corrected to 14:35?

The time zone libraries that I have found so far uses the IANA time zone, e.g. "Europe/Copenhagen", so a library that can convert "Romance Standard Time" to something like "Europe/Paris" would also answer my question. It's acceptable that this conversion is done in .NET and not JavaScript.


Solution

  • The key was to understand that "Romance Standard Time" is a Windows time zone ID. Everyone else uses IANA time zone IDs. They have the region/city format, e.g. "Europe/Copenhagen".

    In .NET 6 it's possible to convert between the formats. Here's the code example from Date, Time, and Time Zone Enhancements in .NET 6.

    // Conversion from Windows to IANA when a region is unknown.
    string windowsTimeZoneId = "Eastern Standard Time";
    if (!TimeZoneInfo.TryConvertWindowsIdToIanaId(
        windowsTimeZoneId, out string ianaTimeZoneId))
    {
        throw new TimeZoneNotFoundException(
            $"No IANA time zone found for "{windowsTimeZoneId}".");
    }
    Console.WriteLine($"{windowsTimeZoneId} => {ianaTimeZoneId}");
    // "Eastern Standard Time => America/New_York"
    
    // Conversion from Windows to IANA when a region is known.
    string windowsTimeZoneId = "Eastern Standard Time";
    string region = "CA"; // Canada
    if (!TimeZoneInfo.TryConvertWindowsIdToIanaId(
        windowsTimeZoneId, region, out string ianaTimeZoneId))
    {
        throw new TimeZoneNotFoundException(
           $"No IANA time zone found for "{windowsTimeZoneId}" in "{region}".");
    }
    Console.WriteLine($"{windowsTimeZoneId} + {region} => {ianaTimeZoneId}");
    // "Eastern Standard Time + CA => America/Toronto"
    

    If you're on an earlier version of .NET you can use TimeZoneConverter. Here's a complete list of the time zones in case you need to build your own converter: windowsZones.json.

    In this answer to another question it is suggested to use NodaTime, so that is probably also a possibility. Convert Windows timezone to moment.js timezone?