Search code examples
datetimetimezonemomentjsdate-conversionnodatime

Moment.js timezone conversion C# .NET


Is there an available c# plugin that handles all the moment.js timezones.

I am having a hard time comparing the dates in the backend since the timezones from moment.js is not the same as the timezones from the .net DateTime.

I need to convert the times to Utc before comparing the different timezoned dates. I am using nodatime and it is able to handle some of the timezones but not all.

Thanks.

EDIT: NodaTime handled most of the timezones but I still need to cover the abbrevations. Is there something available for this?

Thanks.


Solution

  • So NodaTime handled the Olson Timezones as well as GMT+ , UTC and others. For the abbrevations, I settled with this

    public static DateTimeZone GetTimeZone(string id)
        {
            DateTimeZone timezone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(id);
    
            if (timezone == null) {
                var instant = Instant.FromDateTimeUtc(DateTime.UtcNow);
                var source = DateTimeZoneProviders.Tzdb;
                id = source.Ids.Where(i => source[i].GetZoneInterval(instant).Name == id).FirstOrDefault();
                timezone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(id);
            }
    
            return timezone;
        }