System.DateTime没有在服务器上得到更新,但在本地工作正常 [英] System.DateTime is not getting updated in Server but working fine in Local

查看:116
本文介绍了System.DateTime没有在服务器上得到更新,但在本地工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些代码,根据一个位置的国家/地区下拉列表设置申请时间。它在本地工作正常,但在服务器中无法正常部署,请帮助...

I have written some code to set Application time based on a country dropdown based on a location. It works fine in local but not working in Server once deployed please help...

 Date Time AppTime = new DateTime();

  protected void Page_Load(object sender, EventArgs e)
  {  
   AppTime = new DateTime();
   //Time Zone Changes for other Countries            
   TimeZoneInfo tzi1 = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
   DateTime IndTime = DateTime.Now;
   DateTime CurrentUTC = TimeZoneInfo.ConvertTimeToUtc(IndTime);
   DateTime OzzieTime = TimeZoneInfo.ConvertTimeFromUtc(CurrentUTC, tzi1);

  string SelectedCountry = ddlCountry.SelectedValue.ToString();
  string Australia = ConfigurationManager.AppSettings["AustraliaCountryID"];

  if (SelectedCountry == Australia)
  {       
    AppTime = OzzieTime;
  }
  else
  {       
    AppTime = IndTime;
  }

        TextBox1.Text = AppTime.ToString();
        TextBox2.Text = DateTime.UtcNow.ToString();
        TextBox3.Visible = OzzieTime.ToString();;


推荐答案

所有的代码需要做的是: p>

All your code needs to do is this:

string tzid = SelectedCountry == Australia
              ? "AUS Eastern Standard Time"
              : "India Standard Time";

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(tzid);
DateTime appTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);

当然,你认为澳大利亚所有的单一时区都是一个严重的错误。有关详细信息,请参阅本维基百科文章

Of course, you are making a serious mistake in thinking that all of Australia is on a single time zone. See this Wikipedia article for details.

当您将代码发送到生产时,您的代码无法正常工作的原因是您依靠 DateTime.Now 为印度。您的服务器很有可能在另一个时区。例如,将服务器置于UTC的最佳做法。

What's likely the reason that your code didn't work when you sent it to production is that you were relying on DateTime.Now to be India. There's a good chance that your server is in another time zone. For example, it's a best practice to put servers in UTC.

此外,认为您的用户只能在两个不同的时区之一是有点愚蠢。您应该通过 TimeZoneInfo.GetSystemTimeZones()创建一个列表,使用 .Id .DisplayName

Besides, it's a bit silly to think that your users will only be in one of two different time zones. You should probably just create a list via TimeZoneInfo.GetSystemTimeZones(), using the .Id and .DisplayName of each item.

您可能还想确保您的服务器具有最新的Windows更新,或者您已应用手动时区更新

You might also want to make sure that your servers have the latest Windows Updates, or you have applied the Time Zone Updates manually.

这篇关于System.DateTime没有在服务器上得到更新,但在本地工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆