How to Get Utc Offset in JavaScript (Analog of Timezoneinfo.Getutcoffset in C#)

How to get all IANA Timezones from an offset in NodaTime?

You could try the following:

First create a dictionary with all the offsets and the timezones belonging to that offset

Instant now = SystemClock.Instance.Now;
IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
Dictionary<TimeSpan, List<string>> timezonesForOffset = new Dictionary<TimeSpan, List<string>>();
foreach(var id in timeZoneProvider.Ids){
ZonedDateTime zdt = now.InZone(timeZoneProvider[id]);
var timespan = zdt.Offset.ToTimeSpan();
if(timezonesForOffset.ContainsKey(timespan)){
timezonesForOffset[timespan].Add(id);
} else {
timezonesForOffset[timespan] = new List<string> {id, };
}
}

After you have done that you could use the following snippet to get all users within a certain timezone

var timespan2 = new TimeSpan(1,0,0);
var timezonesWithOffset = timezonesForOffset[timespan2];

var usersinTimezone = db.Users.Where(x=> timezonesWithOffset.Contains(x.TimezoneId)).ToList();

That would get all users in the timezone utc+1

Best practice for working with DateTime in C# relative to a user's local timezone

My current thought process is to store these without any time zone into using a DateTime type with Unspecified kind and any usage of this DateTime will be relative to the consumer/stores local time configured on the device.

Yes, that would be appropriate for the scenario you described.

This is sometimes referred to as a "floating time". You might also see it described as "television time", because such scenarios are common in broadcast television with regard to the time a show is aired.

Keep in mind the following:

  • When you store or transmit these values, do not treat them as UTC or associate them with any particular offset. Just use the date and time.

  • When you use these values on the user's device, you can apply the local time zone without actually knowing what that time zone is. For example, in .NET you can use TimeZoneInfo.Local or DateTimeKind.Local. In JavaScript, you can use the Date object, etc.

  • When you use these values elsewhere, such as in a back-end system, scheduler, or administrative UI, you will nee to know the user's time zone ID. Treat the value as belonging to that user's time zone, then convert it to a DateTimeOffset before comparing it to other values. For example:

    TimeZoneInfo tz = TimeZoneInfo.FindBySystemTimeZoneId(userTZ);
    TimeSpan offset = tz.GetUtcOffset(dt);
    DateTimeOffset dto = new DateTimeOffset(dt, offset);

    if (dto >= DateTimeOffset.UtcNow) ...

Getting the client's time zone (and offset) in JavaScript

Using getTimezoneOffset()

You can get the time zone offset in minutes like this:

var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01

How to handle conversion of a DateTime independently of the timezone of the hosting server?

It's usually considered a best practice to

  • Store datetimes as UTC
  • Render this in timezone-aware user friendly format as late as possible

Thus, I would suggest you to not rely on DateTime.Now, but instead consider using DateTime.UtcNow.

Provided you allow each user to determine (through a Preferences/Options panel) to select its own timezone, then you could render an UTC date in the appropriate user-friendly format using:


string timeZoneInfoId = "Romance Standard Time"; // Or any valid Windows timezone id ;-)
var tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfoId);

//Build a DateTimeOffset which holds the correct offset considering the user's timezone
var dto = new DateTimeOffset(TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZoneInfo), timeZoneInfo.GetUtcOffset(utcDateTime));

var formated = string.Format("{0} {1} ({2})", dto.ToString("d"), dto.ToString("T"), dto.ToString("zzz")); //Or whatever format that fits you

Note: You can find the list of all valid Windows timeZoneId here.

Provided you're willing to add a selecting list for the user to choose its rendering timezone, you could use TimeZoneInfo.GetSystemTimeZones() to retrieve a list of TimeZoneInfo, then use the Id property of each TimeZoneInfo as the option's value and call the ToString() method of each TimeZoneInfo to render the option's content.

Your selecting list would render in a similar way than native Windows one:

Windows time zones



Related Topics



Leave a reply



Submit