Jackal
Jackal

Reputation: 3521

How to get the time difference between 2 time spans when past midnight?

I have a start time span and an end time span and I cannot figure out how to get the actual value.

TempoTotalParagem =  DateTime.Now.TimeOfDay - paragem.HoraInico;

well this was working untill it hit midnight.

I searched about this problem and that i needed to add a day.

So I tried

   TempoTotalParagem =  DateTime.Now.TimeOfDay - paragem.HoraInico + DateTime.Now.AddDays(1).TimeOfDay;


paragem.Hora Inicio = 23:14:00
DateTime.Now.TimeOfDay = 01:38

this gives

 TempoTotalParagem =  DateTime.Now.TimeOfDay - paragem.HoraInico + DateTime.Now.AddDays(1).TimeOfDay;

TempoTotalParagem = -19:55:59

I tried changing my variables for datetime but that is a bit complicated because on database the columns are timespans with data already. The purpose is to just count how much time has been since the start date though for the total time column

EDIT

{
    [Table("hParagensRegistos")]
    public class ParagemRegisto
    {
        public int Id { get; set; }

        [DataType(DataType.Time)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
        public TimeSpan HoraInico { get; set; }

        [DataType(DataType.Time)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
        public TimeSpan? HoraFim { get; set; }

        [DataType(DataType.Time)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
        public TimeSpan? TempoTotal { get; set; }

        public int RegistoId { get; set; }
        public Registo Registo {get;set;}

        public int? ParagemPlaneadaId { get; set; }
        public ParagemPlaneada ParagemPlaneada { get; set; }

        public int? ParagemNaoPlaneadaId { get; set; }
        public ParagemNaoPlaneada ParagemNaoPlaneada { get; set; }
    }
}

To avoid confusion here is the actual modal with the timespan properties.

startTime = HoraInico 

Upvotes: 1

Views: 906

Answers (2)

Rufus L
Rufus L

Reputation: 37020

A start time and end time should also contain a date portion. If you include this when you get the difference, the results should be accurate.

For example:

// startTime is Midnight (which is technically the next day at time 0:00:00)
var startTime = DateTime.Today.AddDays(1); 

// endTime is 11:59pm tomorrow
var endTime = DateTime.Today.AddDays(1).AddHours(23).AddMinutes(59);

// Just subtract the values to get the difference
var timeLeft = endTime - startTime;

// timeLeft is 23:59

Some things to take into account here:

  1. All DateTime objects have both a Date and Time portion.
  2. someDateTime.Date (or DateTime.Today) returns the Date portion with the time zeroed out (midnight).
  3. someDateTime.TimeOfDay returns a TimeSpan which represents the elapsed time since midnight and does not include a Date at all.

So if you are just comparing times, without regard to the Date, then the output you were getting was absolutely correct. But if you compare the full DateTime objects, you will get a TimeSpan that accurately reflects the full number of hours/minutes/seconds between the two.

Upvotes: 4

Theraot
Theraot

Reputation: 40180

Do not use TimeSpan.

When your program starts:

paragem.HoraInico = DateTime.Now; // Declare as DateTime not TimeSpan

And then when you need to check:

TempoTotalParagem = DateTime.Now - paragem.HoraInico; // Difference of DateTimes is TimeSpan

Upvotes: 0

Related Questions