-1

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();;
Suresh Savage
  • 425
  • 2
  • 7
  • 16

2 Answers2

0

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.

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.

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.

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

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • appTime is working first time perfectly and once the country is changed the DateTime is not getting changes, the problems seems to be the DateTime varibles only take value once and not after that – Suresh Savage Oct 29 '13 at 03:46
  • I'm not sure I follow... Do you somehow expect your local variables to be persistent beyond their scope?? – Matt Johnson-Pint Oct 29 '13 at 04:06
  • @ Matt Johnson AppTime i delcared it as Global Variable and this is working fine in my local maching the problem arise only after i deploy in server..the Apptime global variable is not updateing in server some how.... – Suresh Savage Oct 29 '13 at 04:27
  • You haven't given us enough information to help with that problem. I'm not even sure from your description if this is a web application, desktop application, mobile phone app, or what... – Matt Johnson-Pint Oct 29 '13 at 04:45
-3

I had the same problem , I build this method:

    private static DateTime getTodayNow(string timeZoneId="")
    {
        DateTime retVal = DateTime.Now;


        DateTime.TryParse(DateTime.Now.ToString("s"), out retVal);

        if (!string.IsNullOrEmpty(timeZoneId))
        {
            try
            {
                DateTime now = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second).ToUniversalTime();
                TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);//"Russian Standard Time");
                retVal = TimeZoneInfo.ConvertTimeFromUtc(now, easternZone);
            }
            catch (TimeZoneNotFoundExceptio‌​n ex)
            {
              //write to log!!
            }
        }
        return retVal;
    }
Izikon
  • 902
  • 11
  • 23
  • the catch will automatically return the default datetime.now, in case someone input a wrong Timezone. Check before you submit a decision – Izikon Oct 28 '13 at 13:17
  • Eating all exceptions is never a good idea. Id doesn't matter you return DateTime.Now or whatever. My point is empty catch or catching `Exception` type is bad. – Sriram Sakthivel Oct 28 '13 at 13:21
  • ha? So if there an exception just stock all the process?? – Izikon Oct 28 '13 at 13:24
  • You should think about avoiding exceptions not eating it. If your input is correct why should you catch exception? – Sriram Sakthivel Oct 28 '13 at 13:31
  • of course! the time zone is modify by regular users, i don't know what they can input to the database... You should know when something has a dynamic user input (even if using a dropdown list to make sure its in the right input) , you should always use try and catch, because you never know what kind of input you going to get from the end user.... – Izikon Oct 28 '13 at 13:33
  • That is why people "validate inputs". My point is not that you missed validation. If you don't have used try/catch that's fine. Novice programmers see this answer and they will start to eat exceptions. – Sriram Sakthivel Oct 28 '13 at 13:40
  • 2
    If you *really* need to eat exceptions, you should make the exception type as specific as possible and the amount of code inside the `try` block as small as possible. In your case this means using `try{TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);}except(TimeZoneNotFoundException)`. – CodesInChaos Oct 28 '13 at 14:43
  • My -1 is for excessive use of `DateTime.Now`, unnecessary string parsing, and ridiculous use of compositing a `DateTime` from its parts. Learn `DateTime.UtcNow`. It is your friend. – Matt Johnson-Pint Oct 29 '13 at 04:25