- Home /
Find time value between two other time values? C#
Hi all,
Anyone know how to do this in a basic 'Hello World' style?
I'm learning C# and am wanting to basically convert my JS over to C#.
How would i check if todays time is within two set times? Example todays time is 13.30.00 and I'm wanting to check if it is between 10.00.00 and 18.00.00?
Any help would be muchly appreciated!
Answer by b1gry4n · Nov 29, 2014 at 03:13 AM
Try converting your time to seconds and compare to the other values in seconds.
13.30.00 = 48600 //currentTime = 48600
10.00.00 = 36000 //lowTime = 36000
18.00.00 = 64800 //highTime = 64800
if(currentTime <= highTime && currentTime >= lowTime){
//the time is between 10 and 18
}
if the current time is less than or equal to high time AND the current time is greater than or equal to the low time, the current time is between the high and low time
Thanks buddy,
How did you work out the syntax to convert it to seconds?
1 hour = 60 $$anonymous$$utes, 1 $$anonymous$$ute = 60 seconds. simple multiplication
convertToSeconds = ((hour * 60) * 60) + ($$anonymous$$ute * 60) + (seconds)
Answer by FlashX · Nov 29, 2014 at 06:21 AM
I'm back again,
I'm having trouble getting it to read as an int and not a string:
private int currentHour = System.DateTime.Now.ToString("hh");
Ive tried a couple of things but can't work it out.
Any ideas?
Thanks again
ToString function will always return string value and you trying to assign it to int.
Thanks Super$$anonymous$$asterBlasterLaser,
Ive tried removing the "ToString" element however I'm too noobish to get the gist as to how to work it. Would you $$anonymous$$d showing me the syntax? $$anonymous$$y head hurts Lol!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with a faster clock? 1 Answer
Get System date 1 Answer
Cleaning this code up. 2 Answers