Solved

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

Posted on 2015-01-28
9
132 Views
Last Modified: 2015-02-07
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
0
Comment
Question by:javagair
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 40576515
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
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 40576573
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

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40576580
Agreed.  24:00 is generally a bad idea because it then becomes unclear which day you want.

I'm sure you can agree with these statements:

    January 28, 2015 @ 00:00:00 <-- It just rolled over from the 27th to the 28th, we are at the beginning of the 28th

    January 28, 2015 @ 03:45:12 <-- It's 3 hrs, 45 minutes, and 12 seconds into the 28th

    January 28, 2015 @ 23:59:59 <-- It's 23 hrs, 59 minutes, and 59 seconds into the 28th

But what happens in the next second?  Right, it rolls over to the 29th:

    January 29, 2015 @ 00:00:00 <-- It just rolled over from the 28th to the 29th, we are at the beginning of the 29th

But if you allow the time to go to 24:00:00...then what?

    January 28, 2015 @ 24:00:00 <-- ?  Is it the 28th or the 29th?

That's 24 hours AFTER the beginning of the 28th, which is the same as 00:00:00 on the 29th, which means the Day portion of this DateTime instance is incorrect (it would return 28 when it actually represents 29).

Either:
(1) Educate your users so that they enter 00 instead of 24 when they mean midnight.
< or >
(2) Explicitly convert a value of 24 in your input to 00 before you attempt to parse it.
0
 

Author Comment

by:javagair
ID: 40577912
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
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40578042
"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
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 40578127
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

0
 

Author Comment

by:javagair
ID: 40579908
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
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 40580343
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.
0
 

Author Comment

by:javagair
ID: 40595854
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now