Playing with Date-Time and Timezone in Code

Foridul Islam
3 min readOct 26, 2020

If you are a programmer then Date and Time is a regular part of your life. If you want to create a calendar or a bus/train ticket booking application or maybe taking an appointment with a doctor or therapist, you have to be very careful about the date and time as well as the timezone of the customer.

This type of application needs to show the time according to the user’s timezone and calculate the start and end time based on the user’s timezone. The challenge comes when this type of calculation happens in different countries with different timezone, especially the DayLight Saving timezone.

Basics about Date

We can create a date with a new Date(). Also, we can add a parameter to create a date. There also have several methods in a date object to calculate year, month, day, and others from a date object. You can click here to know more methods about date object.

Date-time in a specific timezone

If you have the time with timezone then it’s better to store it in UTC with timezone offset. Because when you need to show it to the customers, you can calculate the original date-time from the UTC date.

If we know the current timezone then we can get the timezone offset value for the date. Suppose for timezone ‘Europe/Zurich’,

var localTimezone = moment(dateTime).tz((‘Europe/Zurich’));

var offset = -(localTimezone._offset/60);

Finally, we can get the original date-time from the offset.

var originalDateTime = new Date(dateTime.getTime() + (3600000*offset));

Here, originalDateTime is the date time in the customer’s timezone.

If we want to calculate the date-time in the customer’s timezone, then we have to store the timezone offset value.

var timezoneOffsetValue = (new Date()).getTimezoneOffset();

If we store the timezoneOffsetValue then it will help us to calculate the original time, especially from the back-end.

int offsetHour = (timezoneOffsetValue/ 60) * -1;

var timeWithOffsetValue = time.AddHours(offsetHour);

Here timeWithOffsetValue is the date-time in the customer’s timezone.

Date-time in current timezone with DST

If you don’t know the timezone of the customer then firstly, you have to know the timezone of the customer. There is an option in the moment to guess the current timezone of the user.

moment.tz.guess();
moment.tz.guess(Boolean);

By default Moment Timezone caches the detected timezone. This means that subsequent calls to moment.tz.guess() will always return the same value.

You can call moment.tz.guess() with an optional boolean argument "ignoreCache". If set to true, the cache will be ignored and overwritten with the new value.

The moment.tz.guess(true) method will also consider the Daylight Saving Time.

var currentTimeZone = moment.tz.guess(true);

var localTimezone = moment(dateTime).tz(currentTimeZone);

var offset= -(localTimezone._offset/60);

var originalDateTime = new Date(dateTime.getTime() + (3600000*offset));

Here, originalDateTime is the date-time in the user’s current timezone.

Date-time in user’s preferred language

Peoples from different countries want to show the date-time in their preferred language in different formats.

The toLocaleDateString() method returns a string with a language sensitive representation of the date portion of this date.

The locales and options arguments are the parameters for the language and date-time formats.

var options = { weekday: ‘long’, year: ‘numeric’, month: ‘long’, day: “2-digit” };

var dateTimeInFR = dateTime.toLocaleDateString(‘fr-FR’, options);

Here, dateTimeInFR shows the date-time in France language. Click Here for more details about it.

Change/Update Time of a Date

Sometimes we need to change or update the time of a date. There is no specific option to set hour or minute in date. But we can update the time of a date in the following way.

TimeSpan timeSpan = new TimeSpan(time.Hour, time.Minute, time.Second);

var updatedDateTime = new DateTime(date.Year, date.Month, date.Day, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);

Here, updatedDateTime will show the date with updated time.

Summary

Knowing how to work with date is a very essential part of a developer. Because, if you want to show the correct time in the correct timezone then it is very important to know about date operations. Any mistake in date-time calculation can show the wrong date-time to the user.

--

--