Customizing the Self-Service Portal Source Code Example #1–Displaying the Local Time on the Portal instead of UTC/GMT

The self-service portal right now always displays datetime values in GMT since we don’t have a way to know which time zone the requesting user is in.  We try to make it clear that the datetime is in GMT time zone like this:

image

image

Unfortunately, I think a lot of end users don’t know what GMT is and even if they do it is a pain to figure out what the local time is from GMT (is it minus 7 hours or 8 hours right now?  what is 4:45 PM minus 7 hours?).  So – for those customers that have the majority or all of their users in a particular time zone you may want to change this behavior so that it shows the local time.  There are three places in the code that you need to modify.  They are in the file called TimeHelper.cs.

public static DateTime GetTimeInUtc(DateTime localTime)
{
    if (localTime.Kind == DateTimeKind.Utc)
    {
        return localTime;
    }

    return localTime.ToUniversalTime();
}

public static string GetTimeAsStringInUtc(DateTime localTime)
{
    if (localTime.Kind == DateTimeKind.Utc)
    {
        return string.Format(CommonResources.TimeInGMT, localTime.ToString());
    }

    return string.Format(CommonResources.TimeInGMT, localTime.ToUniversalTime().ToString());
}

public static string GetTimeAsStringInUtc(DateTime localTime, string format)
{
    if (localTime.Kind == DateTimeKind.Utc)
    {
        return string.Format(CommonResources.TimeInGMT, localTime.ToString(format));
    }

    return string.Format(CommonResources.TimeInGMT, localTime.ToUniversalTime().ToString(format));
}

 

You need to change these two lines to remove the .ToUniversalTime() function call so that it reads:

return localTime;

return string.Format(CommonResources.TimeInGMT, localTime.ToString());
return string.Format(CommonResources.TimeInGMT, localTime.ToString(format));

Then just to make sure that you are being clear to the users what time zone you are displaying you should change the TimeInGMT string resource in CommonResources.resx to indicate the time zone.  For example change:

image

to:

image

Make sure you leave the {0} in there.  That is the place in the string in which the actual datetime value will be substituted by the Format function call.

Keep in mind that this will display the local time of the portal server and will never show the local time of the user requesting the page.

Once you have made these changes build and deploy the portal as described in the documentation that accompanies the portal source download.