Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding time of day functions
Message
 
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01664711
Message ID:
01664746
Views:
29
>>>>Hi,
>>>>
>>>>When I use the following function to get the time of the day:
>>>>
>>>>tsTime1 = new TimeSpan(24, 0, 0);
>>>>// tsTime1 is 1:00:00:00.  And I thought it should be 24:00:00
>>>>
>>>>
>>>>When I use the following time of the day:
>>>>
>>>>tsTime2 = new TimeSpan(24, 60, 0);
>>>>// txTime2 is 1:01:00:00
>>>>
>>>>
>>>>How do I get the time of exactly 24:00:00? So that when I compare it with DateTime.Now.TimeOfDay I have the correct result.
>>>>
>>>>TIA
>>>>
>>>>Update: I found this thread:
>>>>https://stackoverflow.com/questions/246225/best-way-to-create-a-midnight-datetime-in-c-sharp
>>>>
>>>>which shows that midnight time is "00:00:00"
>>>>
>>>>What puzzles me is how do you compare is the current time (defined by an hour and minutes, e.g. 24, 00) compares to this time? That is, is now before the midnight or after?
>>>
>>>Here is an example of what I don't get. Suppose I set the range from the beginning of the day (0 hour) till 23:00 (11 pm). And the current time is midnight. Then I want to see if the current time is within the range. I should not be (since midnight is after 23:00). But the following example show otherwise:
>>>
>>>
>>>
>>>// Begin time = 00:00 hour
>>>tsBeingTime = new TimeSpan(0, 0, 0);
>>>// End time = 11 pm
>>>tsEndTime = new TimeSpan(23, 0, 0);
>>>// Current time = midnight 
>>> tsCurrentTime = new TimeSpan(0, 0, 0);
>>>bool lWithinRange;
>>> lWithinRange = true;
>>>  if ((tsCurrentTime.CompareTo(tsBeingTime) < 0 | tsCurrentTime.CompareTo(tsEndTime) > 0))
>>>      {
>>>               // !! I think this should be the hit but it never gets here.
>>>               lWithinRange = false;
>>>       }
>>>
>>
>>
>>>
>>>What am I missing above?
>>>
>>>UPDATE: What I don't understand is why the following returns False:
>>>
>>>tsCurrentTime.CompareTo(tsEndTime) > 0
>>>
>>>
>>>Update2: It appears that the correct setting for the midnight time is not 0,0,0 but 24,0,0
>>
>>In your sample tsCurrent is equal to tsBeingTime (start of day).
>>
>>
>>Midnight as a timespan from the start of day is 24,0,0. Think TimeSpan as the time passed.
>>
>>You can also write it as:
>>
>>TimeSpan.FromHours(24);
>>TimeSpan.FromMinutes(1440); // 24 * 60
>>TimeSpan.FromSeconds(86400); // 24 * 60 * 60
>>...
>>
>>ie:
>>
>>
>>Console.WriteLine(DateTime.Today + TimeSpan.FromDays(1));
>>Console.WriteLine(DateTime.Today + TimeSpan.FromHours(24));
>>Console.WriteLine(DateTime.Today + TimeSpan.FromMinutes(24*60));
>>Console.WriteLine(DateTime.Today + TimeSpan.FromSeconds(24*60*60));
>>Console.WriteLine(DateTime.Today + TimeSpan.FromMilliseconds(24 * 60 * 60 * 1000));
>>Console.WriteLine(DateTime.Today + TimeSpan.FromTicks(24L * 60 * 60 * 1000 * 10000));
>>
>
>Thank you.

I promise this is the last question :)

When I set the time to 24/midnight (e.g. tsMidnight = new TimeSpan(24, 00, 00);) and look at the value of tsMidnight in the debugger is looks like 1.00.00.00
But when I set the time to any other value it looks more "normal". For example:
ts11pm = new TimeSpan(23,59,0)
the value of ts11pm will look like "23:59:00"
??
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform