Displaying Date/Times Correctly Using the User Timezone in the Infor CRM Web Client

The Infor CRM web client handles date/times a little differently than in the LAN client. When you retrieve a date in the LAN client it returns the date based on applying an offset to the UTC database value based on your computer’s timezone. In the web client it acts a bit differently. Because the web server may be running in a different timezone than the clients it is serving, simply returning a UTC offset based on the web server’s timezone would not give accurate results. Because of this, when you retrieve a date field via the entity model it is returned as its straight UTC value, just like if you looked at it in the database.

When you bind a date field to a date control in the web UI, that date control automatically handles converting the bound UTC value to the correct local time based on the computer’s timezone that the browser is being ran on. So handling date/times on bound controls is easy. But what about if you want to work with date/time values on server-side code? Let’s say you want to place a date value within a string along with other text. Because this code will be running on the web server, you have to know how to handle things in order for the date/time to look correct for the user.

Lets take a look at how to do that.

The first thing to know is that there is a client context service which lets you pass items between the server side and the client side of a web client session. You access that by getting a reference to its service like so:

var context = ApplicationContext.Current.Services.Get<Sage.Platform.Application.IContextService>(true);

One of the items that is set on the client side is the browser’s Timezone upon logging in. With that information we can make a call to our context service reference to get that timezone value which was set from the client side and use it from the server side. Sage has implemented a Timezone class to translate that stored timezone into an object that can be used and that has useful methods.

var timeZone = (Sage.Platform.TimeZone)context.GetContext("TimeZone");

The most relevant method of the TimeZone class is a function to convert a UTC date/time to the local date time. This function accepts a DateTime object and returns a DateTime object that can be used as normal but with the UTC date converted to the “users” local time.

Lets look at an example. Lets say we have an Account with a CreateDate stored in the database with a value of “2022-09-17 17:25:57.000” This means that the record was created at 5:25PM UTC time.

If we were to do something like this:

IAccount acc = this.BindingSource.Current as IAccount;
string myDateTest = string.Format("What is this? {0:MM/dd/yyyy h:mm tt}", acc.CreateDate);

Then myDateTest would return “What is this? 09/17/2022 5:25 PM”

Now, let’s imagine that I am a client running a web browser on a computer set to the USA Central timezone. If I were to utilize the timezone context from the server like this:

IAccount acc = this.BindingSource.Current as IAccount;
var context = ApplicationContext.Current.Services.Get<Sage.Platform.Application.IContextService>(true);
var timeZone = (Sage.Platform.TimeZone)context.GetContext("TimeZone");
DateTime dt = timeZone.UTCDateTimeToLocalTime((DateTime)acc.CreateDate);
string myDateTest = string.Format("What is this? {0:MM/dd/yyyy h:mm tt}", dt);

Then myDateTest would return “What is this? 09/17/2022 10:25 AM” which is the Central timezone equivalent of the UTC value “2022-09-17 17:25:57.000”.

Please note that most dates in the system are nullable DateTime? data types so before you go using that UTCDateTimeToLocalTime check that your date/time has a value to avoid null reference errors.

Also, please note that if you are using this kind of code, and storing the result somewhere, the context of the value returned is only “true” for people running a browser in the same time zone as the user who ran the code and set the value initially. For instance if you are trying to put the date into a memo field, everyone will see “What is this? 09/17/2020 10:25 AM” which will confusing for someone say in California, or New York to know that the 10:25AM time was true for a user in the Central time zone. Instead I would recommend including the timezone used, like this:

IAccount acc = this.BindingSource.Current as IAccount;
var context = ApplicationContext.Current.Services.Get<Sage.Platform.Application.IContextService>(true);
var timeZone = (Sage.Platform.TimeZone)context.GetContext("TimeZone");
DateTime dt = timeZone.UTCDateTimeToLocalTime((DateTime)acc.CreateDate);
string myDateTest = string.Format("{0} {1}",
  timeZone.UTCDateTimeToLocalTime((DateTime)acc.CreateDate).ToString("MM/dd/yyyy h:mm tt"), 
  timeZone.StandardName);

Then myDateTest would return “What is this? 09/17/2022 10:25 AM Central Standard Time” which allow people to understand what that time referred to.

Want content like this delivered to your inbox? Sign up for our newsletter!
ABOUT THE AUTHOR

Kris Halsrud

Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.

Submit a Comment

Your email address will not be published. Required fields are marked *