0

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.

Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
  • Do you want it for both .NET and JS? – MrJami Jan 06 '22 at 13:46
  • Either .NET or JS would have been acceptable. I ended up answering my own question, because I found out that you can convert to IANA time zones in .NET 6. (I am actually quite surprised that I didn't stumble upon a JavaScript library that could do the conversion.) – Jan Aagaard Jan 10 '22 at 08:33
  • You might take a look at my answer, you could use moment.js for javascript as a library – MrJami Jan 10 '22 at 09:14
  • Moment.js does not solve this issue, @MrJami, because it uses IANA time zones, and what I have is a Windows time zone. (This is also the reason that I didn't accept your answer.) – Jan Aagaard Jan 11 '22 at 08:59
  • Ah I see, I am sorry for that. I would take a look on it and let you know – MrJami Jan 11 '22 at 09:06
  • I updated my answer and added a library to it :) there you go. – MrJami Jan 11 '22 at 09:25
  • 1
    Oh - I hadn't noticed that. https://github.com/rubenillodo/windows-iana is pretty neat. Thanks for linking to it. – Jan Aagaard Jan 11 '22 at 11:39

2 Answers2

2

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.

This answer to this questions suggests using NodaTime, so that is probably also a possibility: Convert Windows timezone to moment.js timezone?

Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
1

JavaScript

If you want to do it with Javascript, there is multiple Libraries. I am most familiar with moment.js, however they are deprecated, which means you might want to use another library if you are working on a new project.

List of suggested libraries by moment.js.

Anyways, if you wish to work with moment and convert to timezones, you could easily do that using Moment-Timezone. All supported timezones.

For example:

const moment = require('moment-timezone');
const timeZone = "Europe/Paris";

const ISOString = "2022-01-06T13:35:00Z"; //2022-01-06T13:35:00Z
const momentDefault = moment(ISOString).format(); //2022-01-06T14:35:00+01:00
const momentTz = moment.utc(ISOString).tz(timeZone).format(); //2022-01-06T14:35:00+01:00

As you see, moment gets the timezone by default and handels the ISOString as a UTC value, so it automatically converts the value to the timezone of the client user.

You still can specifically tell moment to handle the ISOString as a UTC ISOString and then convert it to a specific Timezone you defined. In that way the client's timezone will be ignored.

EDIT: Now I realized that the questioner wants to convert the timezone from Windows to Iana before converting it in Javascript, because most libraries including Moment.js don't support windows timezone ids.

I found a nice library here.

And here is an example how to convert the timezone first and then use moment.js.

import { findIana } from 'windows-iana';
const moment = require('moment-timezone');

const result = findIana('Romance Standard Time');
console.log(result); // ['Europe/Paris', 'Europe/Brussels', 'Europe/Copenhagen', 'Europe/Madrid', 'Africa/Ceuta']
const timeZone = result[0]; //doesn't really matter which one to take, if you are just converting the times.

const ISOString = "2022-01-06T13:35:00Z"; //2022-01-06T13:35:00Z
const momentDefault = moment(ISOString).format(); //2022-01-06T14:35:00+01:00
const momentTz = moment.utc(ISOString).tz(timeZone).format(); //2022-01-06T14:35:00+01:00

.NET C#

To convert a time to a specific timezone in C#, you dont need any extra packages, you can simply do that as following:

var isoString = "2022-01-06T13:35:00Z";
var utcDate= DateTime.Parse(isoString);
var timeZone = "Romance Standard Time";
var date = TimeZoneInfo.ConvertTime(utcDate, TimeZoneInfo.FindSystemTimeZoneById(timeZone)); //01/06/2022 14:35:00

Here you can find list of all supported timezoneIds

MrJami
  • 685
  • 4
  • 16