Link to home
Start Free TrialLog in
Avatar of javagair
javagairFlag for United States of America

asked on

why does the following code report that this is not a legitimate 24 hour time?

the time selected is 24:00 which is midnight, I know many people subtract or add 1 minute to avoid this problem, but 24:00 is the correct time for midnight on a 24 hour clock.
if I put in 23:59 it works fine but is technically incorrect.
is there a syntax error with the descriptor?

thanks gary
  Try
                    time1 = DateTime.ParseExact(datestub & DirectCast(CustomersGridView.Rows(i).FindControl("ddlin"), DropDownList).Text, "yyyy-MM-dd H:mm", CultureInfo.InvariantCulture)
                Catch ex As Exception
                    Message.Text = "enter a number in 24 hour format for start time ." & CustomersGridView.Rows(i).Cells(0).Text
                    message2.Text = Message.Text
                    Return False
                End Try
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

23:59:59 is the highest then it rolls over to 0:0.00 there is no 24:00:00 since 24:00:01 is incorrect. Its been that way for as long as I know.. When I was in charge of RBFS (secondar) and Cesium FS (primary) the digital display NEVER showed 24:00:00 but 00:00:00 then 00:00:01.  The new day starts at 00:00:00 vice the day ending at 24:00:00
I agree.  There is no 24:00:00  

Midnight is 00:00:00

           time1 = DateTime.ParseExact("2015-01-01 23:59:00", "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
            time1 = time1.AddMinutes(1);
            this.textBox1.Text = time1.ToString(System.Globalization.CultureInfo.InvariantCulture);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of javagair

ASKER

Mike I understand what you are saying but if I have a dropdownlist on my asp.net webpage, it should both be able to start the day and finish the day without making it look funny.  If you were going to tell someone to met you at midnight in a 24 hour clock situation you wouldn't say met me at 0 hundred hours but rather met me at 24 hundred would you not?
from a math perspective, if I go to work at 23:00 hours and I go home at midnight that would be 24 - 23 or 1hr.
if I go to work at midnight and leave at 1:00 that would be 1 - 0 or 1.
So in all reality 24:00 is the end of the day and 0:00 is the beginning of the next day, even though they are the same place in time.

gary
"If you were going to tell someone to met you at midnight in a 24 hour clock situation you wouldn't say met me at 0 hundred hours but rather met me at 24 hundred would you not?"

Actually I would say zero hundred hours; and I've give the correct date so there is no ambiguity.  I'm a military veteran and my wife is still active duty for 21+ years.  If the person is a civilian, then that would probably be followed with "Yes, I mean when x day rolls over into y day at midnight."  ;)

Feel free to put 2400 on your dropdown...just make it VERY CLEAR in your user interface somehow what that means with respect to the dates.

...and you'll still have to convert that to 24 to 00 to get the Parsing to work.

Just to drive the point home:

    DateTime dt = DateTime.Today;
    Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
    dt = dt.AddHours(24);
    Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));

produces:

    2015-01-29 00:00:00
    2015-01-30 00:00:00

and not:

    2015-01-29 00:00:00
    2015-01-29 24:00:00
I understand you want the 24 because some analog clocks actually have 24 on them, but the computer functions do not work like that.  (unless you do something special as Mike suggested)


Based on that math logic, what happens if  you start at midnight and work 1 hour?

24 - 1 = 23

There are functions to handle the time calculations.  

DateTime time1;
time1 = DateTime.ParseExact("2015-01-01 01:00:00", "yyyy-MM-dd HH:mm:ss",  System.Globalization.CultureInfo.InvariantCulture);
time1 = time1.AddHours(-1);
this.textBox1.Text = time1.ToString(System.Globalization.CultureInfo.InvariantCulture);

Open in new window


Result is 01/01/2015 00:00:00

Also, If you try to manually assign  24:00:00 you get an error.
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

Open in new window



One possible work around...  Not tested fully but builds on Mike's Idea.

String s = "2015-01-01 24:00:00";
DateTime time2;
if (s.Contains(" 24:")){s = s.Replace(" 24:", " 00:");}
time2 = DateTime.ParseExact(s, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
String q = time2.ToString(System.Globalization.CultureInfo.InvariantCulture);
if (q.Contains(" 00:")){q = q.Replace(" 00:", " 24:");}
this.textBox1.Text = q;

Open in new window

I all ready followed mikes suggestion. I was just commenting on what people expect to see and do.  Web sites are not built in a vacuum of programmers.

gary
Also x-military and I was responsible for maintaining system time and had 2 Frequency Standards (Rubidium Vapour and Cesium) and since the geostationary satellites were below the horizon we used Loran-C as a check.  Note: if the time was off by more than 40/1000ths of a second it was useless to me. We had a measured antenna delay and receiver delay and propagation delay  and had to wait for the reports of the transmitter delays and other delays before the time signal left the antenna.  There is no such thing as 24:00:00 as time must always be a unique value so the clocks went from 00:00:00 -> 23:59:59.  

Date/time values in computer parlance is the # of seconds from an epoch date/time which using algorithms is then computed into a human readable time. If one starts work at 23:00 on day 0 and finishes on day 1 0100 we get the following the start time is 82800 and the end time is 90000
90000 - 82800 = 7200 seconds /60/60 = 2 hours.

now if we work from 11PM until midnight which works out to 86400 - 82800 = 3600 seconds = 1 Hour.
agreed and I understand, but I am dealing with 145 women who cringe every time you mention a 24 hr clock.  So unless as suggested I show time as what they will except and then do a conversion in the background I could have a revolt on my hands.
I actually went back to a 12 hr. clock with a drop down menu so they can not change a PM to an AM when they actually meant PM.
thanks for the info!!!!!!!

gary