Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding time of day functions
Message
 
 
To
20/12/2018 18:07:59
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01664711
Message ID:
01664742
Views:
27
>>>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
>
>You've set tsEndTime to 23 hours, 0 minutes, 0 seconds
>You've set tsCurrentTime to 0 hours, 0 minutes, 0 seconds
>
>The expression tsCurrentTime.CompareTo(tsEndTime) would return -1 since tsCurrentTime is shorter than tsEndTime

Exactly. How else can I "simulate" the current time being a midnight?
"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
Reply
Map
View

Click here to load this message in the networking platform