简体   繁体   English

如何显示两个日期之间的计算值,而C#中仅小时和分钟之间的差异超过24小时

[英]How to show a calculated value between two dates with just the hour and the minute above 24 hours of difference in C#

I Have an property called Duration, this property is a calculation of two other properties called StartDate and EndDate. 我有一个名为Duration的属性,此属性是对另外两个名为StartDate和EndDate的属性的计算。

The problem is, I need the difference between those two dates in the format "hh:mm", and I could do that with a lot of codes and solutions I found on the Internet, but I always have the same problem at the end, the day count... 问题是,我需要以“ hh:mm”格式区分两个日期之间的区别,并且可以使用我在Internet上找到的许多代码和解决方案来做到这一点,但是最后我总是遇到相同的问题,天数...

Today I have the following code: 今天,我有以下代码:

[NotMapped]
public string Duration
{
 get
 {
  if (StartDate != null && EndDate != null)
  {
   return (EndDate - StartDate).Value.ToString().Substring(0, 5);
  }
  return null;
 }
 set
 {
  if (value == null) throw new ArgumentNullException(nameof(value));
 }
}

Considering we have the following values of Start and End Dates: StartDate = 2019/03/14 - 10:43 AM EndDate = 2019/03/14 - 11:00 AM 考虑到我们具有以下开始和结束日期值:StartDate = 2019/03/14-10:43 AM EndDate = 2019/03/14-11:00 AM

And it show an output like this: 它显示如下输出:

Duration = 00:16 持续时间= 00:16

But when I have more than 24 hours of difference, it counts the day and I get an invalid value. 但是,当我的时差超过24小时时,它就算一天,并且我得到的值无效。

For Example: StartDate = 2019/03/05 - 03:00 AM EndDate = 2019/03/15 - 04:00 AM 例如:StartDate = 2019/03/05-03:00 AM EndDate = 2019/03/15-04:00 AM

It prints: 它打印:

Duration = 1.01: 持续时间= 1.01:

It's couting the day, and I need it to be in hours:minutes format. 今天很忙,我需要采用小时:分钟格式。

Could anyone help me please? 有人可以帮我吗?

Best not to use SubString. 最好不要使用SubString。

If you subtract a date from another date, you get a TimeSpan 如果您从另一个日期减去一个日期,则会得到一个TimeSpan

You can do ToString() on this or use its properties to display what you want, however since you want to display total hours rather than just the hours component, you'll have to use the individual properties: 您可以对此执行ToString()或使用其属性来显示所需的内容,但是由于要显示总小时数而不是仅显示小时数,因此必须使用各个属性:

 var startDate = DateTime.Now;
 var endDate = startDate.AddHours(30).AddMinutes(20);

 var difference = endDate - startDate;

 var hoursComponent = ((int)difference.TotalHours).ToString("D2");
 var minutesComponent = difference.Minutes.ToString("D2");

 Console.WriteLine($"{hoursComponent}:{minutesComponent}");

Something like this should do it. 这样的事情应该做。 I've put the D2 in to force it to display a leading zero if it's only a single digit. 如果只有一个数字,我将D2放入其中以使其显示前导零。 I cast TotalHours to an int because TotalHours actually returns a double with a fraction component which you don't want. 我将TotalHours转换为int因为TotalHours实际上返回一个带有您不想要的小数部分的double。 I didn't use TotalMinutes because I don't think the Second component is relevant. 我没有使用TotalMinutes因为我认为第二个组件不相关。

This is how to do it: 这是怎么做的:

DateTime startDate = DateTime.Now;

DateTime endDate = startDate.AddDays(1).AddHours(2).AddMinutes(3);

TimeSpan difference = endDate - startDate;

int totalHours = (int)difference.TotalMinutes / 60;
int totalMinutes = (int)difference.TotalMinutes % 60;

Console.WriteLine(string.Format("{0}:{1}", totalHours.ToString("D2"), totalMinutes.ToString("D2")));

Output: 输出:

26:03 26:03

Which stands for 26 hours and 3 minutes passed, as you asked. 根据您的要求,经过26小时3分钟。

尝试这个,

hours = (datevalue1 - datevalue2).TotalHours().ToString();

Go with the timespan: 与时间跨度:

TimeSpan span = (EndDate - StartDate);

String.Format("{0}:{1}",span.Hours, span.Minutes);

You can use the ToString method of the TimeSpan to format your output as you wish: 您可以根据需要使用TimeSpanToString方法来格式化输出:

void Main()
{
    DateTime? startDate = new DateTime(2019, 3, 5, 3, 0, 0);
    DateTime? endDate = new DateTime(2019, 3, 15, 4, 0, 0);

    TimeSpan result = (endDate - startDate).Value;
    Console.WriteLine(result.ToString(@"dd\:hh\:mm\:ss"));
}

Output: 输出:

10:01:00:00 10:01:00:00

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

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