我的另一个时间问题:如何计算负值? [英] My another Time question: How to calculate negative value?

查看:91
本文介绍了我的另一个时间问题:如何计算负值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很容易,但我仍然 挣扎.我正在计算两次之间的差异,如果差异大于一个小时,则它应该执行某些任务.问题是,如果第一次是晚上11点之后,我找不到合适的逻辑或方法来处理第二次是 凌晨12点之后.请参阅我的以下情况以获得更好的理解

This could be very easy, but still I am struggling. I am calculating the difference between two times and if the difference is higher than an hour then it should do some task. The problem is I cannot find a good logic or way to handle if First time is after 11PM  and Second time is just after 12 AM. See my below scenarios for better understanding

在这种情况下,它工作正常

For this scenario, it is working fine

TimeSpan t1 = 13:25;

TimeSpan t1=13:25;

TimeSpan t2 = 14:45;

TimeSpan t2=14:45;

TimeSpan diff = t2.Subtract(t1);

TimeSpan diff=t2.Subtract(t1);

TimeSpan max = TimeSpan.Parse("01:01:00");

TimeSpan max=TimeSpan.Parse("01:01:00");

if(diff> max)

if (diff>max)

{

  //程序在If条件下执行任务,因为时间差大于一个小时

  //The program execute the tasks in If condition as the time difference is greater than an hour 

}

但是在这种情况下,IF条件应该会失败...但是它不会失败并正在执行

TimeSpan t1 = 23:05;

TimeSpan t1=23:05;

TimeSpan t2 = 00:00; //这只是5分钟后

TimeSpan t2=00:00; //This is just 5 minutes later

TimeSpan diff = t2.Subtract(t1);

TimeSpan diff=t2.Subtract(t1);

TimeSpan max = TimeSpan.Parse("01:00:00");// 最大 允许的时间

TimeSpan max=TimeSpan.Parse("01:00:00");//Maximum allowed time

TimeSpan min = TimeSpan.Parse("00:00:00");//允许的最短时间

TimeSpan min=TimeSpan.Parse("00:00:00");//Minimum allowed time

if(diff> max || diff< min)//差异仅5分钟,但是程序假设时间差距很大

if (diff>max || diff<min) //The diff is just 5 minutes only, but the program assuming the time gap is big

{

  //做一些伙伴.程序不应该来这里

  //Do something buddy. Program should not come here

}

区别在于负数(负)

推荐答案

您的时间跨度是错误的,因为您假设时间跨度为00:00是第二天的午夜,而事实并非如此.  您需要其中包含天数部分:

Your timespan is wrong because you are assuming a timespan of 00:00 is midnight the next day, and it's not.  You need to have a days component in it:

 

		// current day
		TimeSpan t1 = new TimeSpan(0, 23, 55, 0);

		// needs to be +1 day ahead
		TimeSpan t2 = new TimeSpan(1, 0, 0, 0);

		var diff = t2 - t1;

		Console.WriteLine(diff);


这篇关于我的另一个时间问题:如何计算负值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆