简体   繁体   中英

How to calculate amount of hours between two points in c#/Code review

Hello everyone I have a small programming problem which is probably a lot easier than i think. So I need to set the time to install Timespan opbject below to be 24 + time left to the next 4 pm. The below is C# pseudo code, it was written in notepad because at work I don't have an IDE, i also don't have much experience in programming using dates. i think my alghorithm will work but i guess there is a milion easier ways to do it. Please have a look:

//I need to make a timespan object which has 24 hours from current time + time left to the next 4pm

//The context is time to install, which user should see
Timespan TimeToInstall = new Timespan(23,59,59)

//Now I am taking the current time
Current = DateTime.Now

//Now I add the 24 hours to the current in order to create the next day date
Current.Add(TimeToInstall)

//Now creating the 4 PM on the next day
DateTime pm4 = new DateTime(Current.year,Current.month,Current.Day,16,0,0)

//Now checking if current is above or below 4 pm
if(Current.TimeOfDay < pm4){
    TimeToInstall = TimeToInstall + (pm4 - Current)
}else if(Current.TimeOfDay > pm4){
    pm4.AddDays(1)
    TimeToInstall = TimeToInstall + (pm4 - Current)
}else {
    //24 hours has passed and it is 4 pm so nothing to do here
}

TimeSpan can be negative. So just substract the TimeSpan for 4PM with current TimeOfDay , if you get negative value, add 24 hours.

var timeLeft = new TimeSpan(16, 0, 0) - DateTime.Now.TimeOfDay;
if (timeLeft.Ticks<0) 
{
    timeLeft = timeLeft.Add(new TimeSpan(24,0,0))
}

Based on your code:

DateTime now = DateTime.Now; 

DateTime today4pmDateTime= new DateTime(now.Year, now.Month, now.Day, 16, 0, 0);

//Will hold the next 4pm DateTime.
DateTime next4pmDateTimeOccurrence = now.Hour >= 16 ?  today4pmDateTime : today4pmDateTime.AddDays(1);

//From here you can do all the calculations you need
TimeSpan timeUntilNext4pm =  next4pmDateTimeOccurrence  - now;

The answer is really simple, I should have seened this earlier. The solution to these kind of problems is basically modular arithmetic. The client requirment was the popup to show 24+ time to next 4 pm (Don't ask i don't know) so if:

program runs at 13:00 then the clock should display 24 +3 = 27

when 16:00 it should be 24+24 which is 48

when 22:00 it shoould be 24 + 18 which is 42

Now I noticed that:

13 + 27 = 40

16 + 24 = 40

22 + 18 = 40

40 Modulo 24 = 16

So basically if I subtract the current time from 40 then I will be left with the difference:

40 - 13 = 27

40 - 16 = 24

40 - 22 = 18

So what I did is this:

TimeSpan TimeToInstall;                
TimeSpan TimeGiven = new TimeSpan(23, 59, 59);        

DateTime Now = DateTime.Now;
long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks) + TimeGiven.Ticks;
TimeToInstall = TimeSpan.FromTicks(TimeTo4);

EDIT The above was a trap

Corrected:

DateTime Now = DateTime.Now;
if (Now.Hour < 16)
{
    long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks);
    TimeToInstall = TimeSpan.FromTicks(TimeTo4);
}
else
{

    long TimeTo4 = (new TimeSpan(40, 0, 0).Ticks - Now.TimeOfDay.Ticks) + TimeGiven.Ticks;
    TimeToInstall = TimeSpan.FromTicks(TimeTo4);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM