tags:

views:

108

answers:

4

I have a begin and start DateTime for a conversion that gets logged at the start and end of this event. I then generate a report that lists a bunch of information about the event including its runtime. I have a column for total time elapsed (in days) and a date representation of start and stop. Everything is in days, I don't care about 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 I can use the ToString("M/d/yyyy") method of datetime to print each individual date, but I'm hoping for an easy way to print two dates in a similar format.

A: 

I think you just have to code it, it is just 2 if statements... is year different, is month different, else.

Something like

if (d1.year == d2.year)
{
  if (d1.month == d2.month)
     print format 1
  else
     print format 2
}
else
  print format 3
Hogan
+3  A: 

Your rules are pretty simple to translate to code, no need to get fancy.

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");
    }
}

Demos:

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)));
Anthony Pegram
Exactly what I was thinking. You could streamline it a bit by selecting format strings based on the date logic and doing the format in the return statement.
tzerb
This solution ignores the case that `startDate` and `endDate` are the same day.
JaredPar
Thanks. I had absolutely no idea DateTime had Year and Month properties. I thought this was going to have to be done in regex. I wish I had VS instead of notepad and csc.exe, but my for some reason I'm not cool enough.
Shawn
@Shawn, google Visual Studio Express and download free versions of the software. They're not as full featured as the regular versions, but they'll let you use the IDE and compile and even distribute. They're not bad at all.
Anthony Pegram
A: 
// 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);
Andrew
+2  A: 

You didn't specify how it should be printed out if they are in fact the same date. Going to assume in that case that only a single date should be printed out.

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");
  }
}
JaredPar