简体   繁体   中英

Client Timezone in ASP.NET

I have a table that needs to display a date/time in the client's timezone. However, I am having a hard time finding a way to do this effectively. I know I can use client side javascript to get the timezone (and that this method is a little flaky), but I need it in the Page_Load event, so I can't call javascript beforehand.

I can get it from a separate page (at login for example), but that doesn't always work, because sometimes people use bookmarks directly into internal pages, and bypass logon with a cookie.

So I am left a few choices: 1. Have a cache per user for the last timezone that I fill up at every opportunity from a postback with no guarantee it will be right)

  1. Try some weird IP geolocation hack

  2. Have a user profile that allows the user to set their timezone (again, if they travel this won't always be right either)

  3. Try some funkly page redirect to force the postback, (but some browsers disable page redirects)

  4. Have the user explicitly set the TZ

  5. Do the tz formatting in Javascript

None of these are ideal, it seems to me to be info the Browser should be providing the server. Does anyone have any other suggestions?

If possible, display times relatively . Rather than showing a particular time, use prose like, "5 minutes ago," or "Last week."

If you must display an absolute time in the client's time zone, based on your comments I'd go with option #5, sending UTC time down to the browser and then displaying local time using JavaScript. From your description it sounds like users can visit the page(s) that need to display the time in their time zone without having logged in, so storing it in the user profile seems inadequate.

I discuss the relative time display concept in more detail in Advice for Storing and Displaying Dates and Times Across Different Time Zones , as well as provide a simple extension method on the DateTime structure for adding a ToRelativeDateString method so that you can write code like:

string relativeTime = myDateTimeVariable.ToRelativeDateString();

Hope this helps...

I would use a mixed strategy:

  1. Users can set the timezone in their profiles
  2. Default is "auto" which means: format with JavaScript

Keep the timezone in the user profile however on each page that you display it (or in the master page) give the user the ability to adjust it. Once its adjusted allow it to persist for the remainder of the session and if they want it set then they will need to set it in their profile.

5,2,3,4,1可能是您选择的最佳顺序。

只需以UTC格式输出所有时间,然后在客户端转换为本地时间。

I would have to agree with Scott Michael. Display relative time, if you need absolute times your browser already knows how to localise them from UTC.

But if you're looking for detecting timezones robustly with javascript (for use server side); check out jsTimezoneDetect . It will give you an Olsen timezone key that you can use with server side normalizations of datetimes.

I thought I'd give you an update. I did take up the various suggestions of formatting the date on the client side (or more specifically reformatting it.) As google fodder I have put a full explanation of this at the end of this post.

However, the problem is that Date.toLocaleString takes no parameters to control the formatting, and so, in my US locale anyway, I get a big bulky string "Saturday, November 27, 2010 3:58:38 PM" This is way too bulky, I want a compact format like Sat 11/27/10 3:58PM" but there is not way to control it AFAIK.

So I am going to modify to try to cache the time zone in the session based on input from the various key pages posting back the timezone offset, and include the ability to modify the tz in the user's profile. It is not nice, but it is the best I can come up with. Like I say, this really should be included in the browser's http headers.

----- Formatting on the client side ------------

So I have labels like this (in a ListView FWIW):

<asp:Label ID="TimeLabel" runat="server" class="UTCTimeCell Hidden">
    <%# Eval("when") %> UTC
</asp:Label>

Note you have to include UTC for the timezone since the default ASP.NET formatter does not include it. (Here I am assuming you are storing your dates as UTC, which in nearly all cases you should.) Note Hidden is a standard css class I used with display:none.

Then I define the following functions in my utilties javascript (obviously I use jQuery...)

function timeFormatLocal(timeStr) {
    var dt = new Date(timeStr);
    return dt.toLocaleString();
}

function timeReformatLocal(selector) {
    $(selector).each(function () {
        $(this).html(timeFormatLocal($(this).html()));
    });
}

then in my page ready event I use something like this:

timeReformatLocal(".UTCTimeCell");
$(".UTCTimeCell").removeClass("Hidden");

This reformats those label cells to the locale based format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM