C # Recording time after two days as a string

I have the beginning and the beginning of a DateTime for the conversion, which is logged at the beginning and at the end of this event. Then I create a report that lists a bunch of information about the event, including its execution time. I have a column for the total elapsed time (in days) and a representation of the start and stop dates. All in days, I don’t care hours / minutes / seconds.

If the start time is 9/29/2010 and the end time is 9/31/2010 I want to print:

9/29-31/2010

If the start time is 9/29/2010 and the end time is 10/2/2010 I want to print:

9/29-10/2/2010

If the start time is 12/29/2010 and the end time is 1/2/2011 I want to print:

12/29/2010-1/2/2011

I know that I can use the ToString date and time method ("M / d / yyyy") to print each individual date, but I hope for an easy way to print two dates in a similar format.

+1
source share
4 answers

Your rules are quite simple to translate into code, no need to invent.

static string GetDateRangeString(DateTime startDate, DateTime endDate)
{
    if (endDate.Year != startDate.Year)
    {
        return startDate.ToString("M/d/yyyy") + "-" + endDate.ToString("M/d/yyyy");
    }
    else if (endDate.Month != startDate.Month)
    {
        return startDate.ToString("M/d") + "-" + endDate.ToString("M/d/yyyy");
    }
    else
    {
        return startDate.ToString("M/d") + "-" + endDate.ToString("d/yyyy");
    }
}

Demonstrations:

Console.WriteLine(GetDateRangeString(new DateTime(2010, 9, 29), new DateTime(2010, 9, 30)));
Console.WriteLine(GetDateRangeString(new DateTime(2010, 9, 29), new DateTime(2010, 10, 30)));
Console.WriteLine(GetDateRangeString(new DateTime(2010, 9, 29), new DateTime(2011, 1, 30)));
+3
source

You did not specify how to print it if they actually match the date. Assuming that in this case you need to print only one date.

static string DateRangeToString(DateTime left, DateTime right) { 
  if ( left.Year != right.Year ) {
    return String.Format("{0}-{1}", left.ToString("M/d/yyyy"), right.ToString("M/d/yyyy"));
  } else if ( left.Month != right.Month ) { 
    return String.Format("{0}-{1}", left.ToString("M/d"), right.ToString("M/d/yyyy"));
  } else if ( left.Day != right.Day ) { 
    return String.Format("{0}-{1}", left.ToString("M/d"), right.ToString("d/yyyy"));
  } else {
    return left.ToString("M/d/yyyy");
  }
}
+2
source

I think you just need to code it, it's only 2 if the statements ... are different year, another month, yet.

Sort of

if (d1.year == d2.year)
{
  if (d1.month == d2.month)
     print format 1
  else
     print format 2
}
else
  print format 3
0
source
// Precondition: dt1 < dt2
string dateString1 = dt1.ToString("M/d/yyyy");
string dateString2 = dt2.ToString("M/d/yyyy");
if (dt1.Year == dt2.Year)
{
    dateString1 = dateString1.Substring(0, dateString1.Length - 5);
    if (dt1.Month == dt2.Month)
    {
        dateString2 = dateString2.Substring(dt1.Month < 10 ? 2 : 3);
    }
}
string finalValue = String.Format("{0}-{1}", dateString1, dateString2);
0
source

All Articles