简体   繁体   English

午夜过后如何获得两个时间跨度之间的时差?

[英]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 

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. 所有DateTime对象都具有DateTime部分。
  2. someDateTime.Date (or DateTime.Today ) returns the Date portion with the time zeroed out (midnight). someDateTime.Date (或DateTime.Today )返回Date部分,其中时间清零(午夜)。
  3. someDateTime.TimeOfDay returns a TimeSpan which represents the elapsed time since midnight and does not include a Date at all. someDateTime.TimeOfDay返回一个TimeSpan ,它表示自午夜以来的经过时间,并且根本不包含Date

So if you are just comparing times, without regard to the Date , then the output you were getting was absolutely correct. 因此,如果您只是比较时间,而不考虑Date ,那么您得到的输出是绝对正确的。 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. 但是,如果您比较完整的DateTime对象,则将获得一个TimeSpan ,它可以准确反映两者之间的小时/分钟/秒的总数。

Do not use TimeSpan . 不要使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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