locked
Time format not supporting RRS feed

  • Question

  • User1052662409 posted

    Deal All,

    I am trying to restrict users to do attendance after 9:00 AM. I put the below code.

      DateTime attendancetime = new DateTime();
                attendancetime = DateTime.Now;
    
      // did somtning
      
      
                    if (attendancetime <= Convert.ToDateTime("09:00 AM"))
                    {
                      // do something
                    }
                    else
                    {
                    
                    }

    I was wondering that it is working fine in my local machine, but when I host the website the condition always goes into else part.

    Please help.

    Thanks

    Wednesday, January 9, 2019 3:42 AM

Answers

  • User303363814 posted

    If your users are all in the same timezone (but it seems that your server is in a different zone) then you first need to get the TimeZone information.  Then grab the current UTC time and, finally convert to your desired timezone.  Something like this.

    var myZone = TimeZoneInfo.FindSystemTimeZoneById("the name of the time zone you are interested in");
    var utcNow = DateTimeOffset.UtcNow;
    var myCurrentTone = utcNow.ToOffset(myZone.GetUtcOffset(utcNow));

    You can now check, for example, what hour it is in the timezone of interest with myCurrentTime.Hour.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 11, 2019 9:30 AM

All replies

  • User-1716253493 posted

    AFAIK, each culture has it's own format. You can specify what culture is used for the time string

    using System.Globalization;
    if (attendancetime <= Convert.ToDateTime("09:00 AM", new CultureInfo("en-US)))

    I'm not sure en-US is using AM/PM or not, try use "09:00" for AM and "21:00" for PM if en-US culture format not work

    Wednesday, January 9, 2019 7:37 AM
  • User1052662409 posted

    oned_gk

    if (attendancetime <= Convert.ToDateTime("09:00 AM", new CultureInfo("en-US)))

    Not working, I tried both way 12 and 24 format. Nothing is working.

    Even I tried the below methods, still no success.

    TimeSpan start = new TimeSpan(17, 0, 0); //5 o'clock
    if (DateTime.Now.TimeOfDay <= start)
    {
      // do something
    }
    else
    {
      //
    }

    It always goes in if block.

    Wednesday, January 9, 2019 11:57 AM
  • User753101303 posted

    Hi,

    Rather than reading the code see which value you have in TimeOfDay (show maybe this value in a label).  For now this code depends on the time zone in which the server is found.

    To fix this you'll likely need to use https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo?view=netframework-4.7.2 to first get your own local time (rather than the server local time) before doing the test.

    Edit: all users are in the same time zone or could it happen soon? In this case a JavaScript side solutiion could be perhaps easier. You want to prevent logging to your site outside working hours ???

    Wednesday, January 9, 2019 12:43 PM
  • User1052662409 posted

    PatriceSc

    all users are in the same time zone or could it happen soon? In this case a JavaScript side solutiion could be perhaps easier. You want to prevent logging to your site outside working hours ???

    Yes, I want to set a fix time to restrict users. Which time format I need to use in my program. Please suggest.

    Thanks

    Thursday, January 10, 2019 3:37 AM
  • User753101303 posted

    No what I meant is that I believe that your server is for example in Japan while you are in Italy. So when running locally it gives you the result you expect but once deployed to your Japanese server your code is testing the time in Japan.

    This is why I suggested to show the value (I like to go step by step) and anyway it's the logical action to understand why a comparison doesn't give the expected result.

    All users are using always this site from the same time zone. Is this possible for example that someone might need to access that when on a business trip ? You could then lock it out if having a meeting in Japan while Italy is sleeping.

    If sure then you could test https://docs.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttimefromutc?view=netframework-4.7.2#examples and see if it shows now the  local time  you expect. To reiterate it will give the time in "Italy".

    If this site is used from other places, it could be best to see which local time it is on the browser side.

    Thursday, January 10, 2019 8:40 AM
  • User303363814 posted

    If your users are all in the same timezone (but it seems that your server is in a different zone) then you first need to get the TimeZone information.  Then grab the current UTC time and, finally convert to your desired timezone.  Something like this.

    var myZone = TimeZoneInfo.FindSystemTimeZoneById("the name of the time zone you are interested in");
    var utcNow = DateTimeOffset.UtcNow;
    var myCurrentTone = utcNow.ToOffset(myZone.GetUtcOffset(utcNow));

    You can now check, for example, what hour it is in the timezone of interest with myCurrentTime.Hour.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 11, 2019 9:30 AM