简体   繁体   中英

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.

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...

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

And it show an output like this:

Duration = 00:16

But when I have more than 24 hours of difference, it counts the day and I get an invalid value.

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

It prints:

Duration = 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.

If you subtract a date from another date, you get a 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:

 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. I cast TotalHours to an int because TotalHours actually returns a double with a fraction component which you don't want. I didn't use TotalMinutes because I don't think the Second component is relevant.

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

Which stands for 26 hours and 3 minutes passed, as you asked.

尝试这个,

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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