Web application time zone issue

We have an ASP.Net 2.0 web application that works with a server in the Midwest (Eastern Standard Time). At the moment, all our clients are in the same time zone as the server. We are launching another server online in Arizona (Mountain Standard Time).

We save all our time in a SQL 2005 database using C # codebehind DateTime.UtcNow.

During testing, we encountered some time zone conversion problems. Our problem is that in the web browser, our time displays "Mountain Standard Time" instead of the time zone that we are testing, from which "Eastern Standard Time" comes.

When we enter new information, it is stored in UTC format in the database, but when we move on to viewing this information in a browser, it displays Mountain Standard Time. Below is the code that takes the UTC value from the database and displays it in the browser.

lblUpdatedDate.Text = Convert.ToDateTime(dr["UpdatedDate"]).ToLocalTime().ToString();

The above code returns Mountain Standard Time, where the server is located, and not the Eastern Standard Time, in which the browser works. How do we get time to display where the user is?

+5
source share
7 answers

. , , -. UTC, . , , 3 . , , , ( ). , , , TimeZoneInfo. .

+6

. .

  • UTC , UTC
  • JS ( , -)
  • MSDN Timezone
+3

.NET 3.5 , , TimeZoneInfo . .NET 3.5, P/Invoke TimeZone, 3.5. (TimeZoneInfo .., , , .)

, , - . - . ( .)

+2

- ASP.NET. .

JavaScript, document.write. JavaScript GMT. , JavaScript /.

+2

GMT.

:

Javascript:

var dt = new Date();
var diffInMinutes = -dt.getTimezoneOffset();

# :

string queryStr = Request.QueryString["diffInMinutes"];
int diffInMinutes = 0;
if (Int32.TryParse(queryStr, out diffInMinutes))
{
    clientTime = serverTime.ToUniversalTime().AddMinutes(diffInMinutes);
}
+1

, webservices, .

, ...

(SetAllDateModes), - - (Foreach) , Mode:

foreach (DataColumn dc in dt.Columns)
{
  if (dc.DataType == typeof(DateTime))
  {
    dc.DateTimeMode = dateMode;
  }
}
SetAllDateModes(dt, DataSetDateTime.Unspecified);

DateSetDateTime.Unspecified , .

0

I thought the general idea was not to localize the date and time until you present it to the user. Therefore, if a person is not going to read it, he remains in UTC, GMT, CUT. Another note uses the Date / Time library, so you don’t have to worry much about problems with daylight saving changes.

0
source

All Articles