Time difference Timespan in 24hrs format

Asked

Viewed 914 times

3

I am asking this question because I have not located any topic with my problem, which is very similar to others.

I have 2 fields TimeSpan? one of which relates to check-in time and the other to departure time.

I want to know the time difference, so if the person works from 8:00 to 10:00, he worked 2 hours.

This is very simple, maybe with a Compare or a sub I can extract this total value, but when I inform that the person entered at 17:00 and left at 16:00 he returns me 1, being that he worked 23 hours

How can I make that calculation?

  • 1

    if he worked from 16(4 hours) and left at 17(5 hours), he will return one hour even if you are trying to say that he went in one day and went out the other you also have to compare the day.

  • I got confused in the text, if he entered at 17 and left at 16 o'clock, already corrected in the text.

  • "when I inform that the person entered at 16:00 and left at 17:00 he returns me 1, being that he worked 23 hours" so I understood this is right, he should return 23 if the person enters at 17 and leaves at 16, no?

  • @Exactly, that’s what I’m having trouble with, knowing from 17 to 16 hrs

  • 1

    http://stackoverflow.com/a/4946393/4551469 sees if this way works var hours = (datevalue1 - datevalue2).TotalHours;

  • Theoretically the values will never be fixed so she can report that is input 15hrs and exit at 18hrs, how much can also be from 18hrs until 17hrs the other day. I would have to make a check if the date left was less than the date of entry and add 1 day on the date left, it would be this ?

  • 2

    There is confusion there. You need as a response a period, a time interval, then the answer will be a TimeSpan. Data entry is the time of entry and time of exit, so they are two points in time, IE, should be DateTime. When it starts wrong it’s hard to make it right.

  • I figured it out, you were right, all I had to do was convert to DateTime and add 1 day to the check-out date when it is less than the check-in date.

  • 1

    And I used the Totalhours @rLinhares reported. Thank you very much!

Show 4 more comments

2 answers

2


There’s a mess there.

You need as an answer a period, a time interval, then the answer will be a TimeSpan.

Data entry is the time of entry and time of exit, so there are two points in time, that is, they must be DateTime.

When it starts wrong it gets hard to do right. Turn these hours into DateTime and then just do a subtraction.

using System;
using static System.Console;

public class Program {
    public static void Main() {
        var objeto = new AlgumaClasse();
        objeto.HoraEntrada = DateTime.Now;
        objeto.HoraSaida = DateTime.Now.AddHours(1).AddMinutes(43).AddSeconds(22);
        WriteLine($"Permaneceu {(objeto.TempoPermanencia().ToString(@"hh\:mm"))}");
    }
}

class AlgumaClasse {
    public DateTime HoraEntrada { get; set; }

    public DateTime HoraSaida { get; set; }

    public TimeSpan TempoPermanencia() => HoraSaida - HoraEntrada;
}

Behold working in the ideone. And in the .NET Fiddle. Also I put on Github for future reference.

  • Good, the text is correct but the code is not the solution, it lacks a lot but thanks anyway!

  • 3

    I answered on top of what you put in the question that lacks enough thing.

0

Timespan has the option to have the days as well. In its constructor has an overload that you inform (days, hours, minutes, Seconds) and if you inform the amount of days the calculation will be more accurate.

Example:

    TimeSpan t1 = new TimeSpan(0,17,0,0);
    TimeSpan t2 = new TimeSpan(1,16,0,0); //Repare que aqui tem um dia.

    var result = t2-t1;
    Console.Write(result.ToString()); // Resultado será 23:00:00

If in the unstable t2 variable passing the first parameter as zero, you will see that the result will be -01:00:00. The above code can be checked here https://dotnetfiddle.net/tUO2AI

So if you persist these fields somewhere, at the time of persisting you should consider the amount of days in your timespan.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.