views:

84

answers:

2

i have this code to take a time in the past and generate a readable string to represent how long ago it was.

  1. I would have thought Timespan.Hours would give you hours even if its multiple daye in the past but it looks like it breaks it down into its seperate components (days, months, etc). How would i get total hours ago (even if its more than 1 day?

  2. Is there any cleaner way to write this type of code below as it seems pretty spagetti-ish.

Here is the code

        DateTime when = GetDateTimeinPast();
        TimeStamp ts = DateTime.Subtract(when);

        switch (ts.Days)
        {
            case 0:
               if (ts.Hours < 1)
                    b.Append( ts.Minutes + " minutes ago");
               else
                   b.Append( ts.Hours + " hours ago");
                break;
            case 1:
                b.Append( " yesterday");
                break;
            case 2:
            case 3:                
            case 4:

                b.Append( "on " + when.DayOfWeek.ToString());
                break;
            default:
                b.Append(ts.Days + " days ago");
                break;
        }
+4  A: 

Use the TotalHours property or other Total[TimeUnit] properties in the timespan object.

For a timespan of 1:10 (hh:mm), it equates to 1 Hours and 10 Minutes or 1.167 TotalHours and 70 TotalMinutes.

As for cleaning it up, stick to using if/else branches as you had earlier. switch/case will not help you with these conditions, only for specific values. Something like this:

DateTime when = GetDateTimeinPast();
TimeSpan ts = DateTime.Now.Subtract(when);
if (ts.TotalHours < 1)
    b.AppendFormat("{0} minutes ago", (int)ts.TotalMinutes);
else if (ts.TotalDays < 1)
    b.AppendFormat("{0} hours ago", (int)ts.TotalHours);
//etc...
Jeff M
@Jeff M - thanks. Any ideas on the second question or is this above is basically the solution
ooo
A: 

As an alternative, I have a solution that does that beyond days with weeks, months and years. The approach is a bit different It advances from the past to the future, first trying the big steps and if it overshoots switching to the next smaller one.

PeriodOfTimeOutput.cs

flq