1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Helpers
20 /// Simple helper for date formatting
22 public class DateFormatHelper
: AbstractHelper
25 /// Alternative representation of a difference
26 /// between the specified date and now. If within 24hr
27 /// it returns <c>Today</c>. If within 48hr it returns
28 /// <c>Yesterday</c>. If within 40 days it returns
29 /// <c>x days ago</c> and otherwise it returns
30 /// <c>x months ago</c>
32 /// TODO: Think about i18n
35 /// <param name="date">The date in the past (should be equal or less than now)</param>
36 /// <returns></returns>
37 public String
AlternativeFriendlyFormatFromNow(DateTime date
)
39 TimeSpan now
= new TimeSpan(DateTime
.Now
.Ticks
);
40 TimeSpan cur
= new TimeSpan(date
.Ticks
);
42 TimeSpan diff
= now
.Subtract(cur
);
44 if (diff
.TotalHours
<= 24)
48 else if (diff
.TotalHours
<= 48)
52 else if (diff
.TotalDays
<= 40)
54 return String
.Format("{0} days ago", diff
.Days
);
58 return String
.Format("{0} months ago", (diff
.Days
/ 30));
63 /// Returns the difference from the
64 /// specified <c>date</c> the the current date
65 /// in a friendly string like "1 day ago"
67 /// TODO: Think about i18n
70 /// <param name="date">The date in the past (should be equal or less than now)</param>
71 /// <returns></returns>
72 public String
FriendlyFormatFromNow(DateTime date
)
74 TimeSpan now
= new TimeSpan(DateTime
.Now
.Ticks
);
75 TimeSpan cur
= new TimeSpan(date
.Ticks
);
77 TimeSpan diff
= now
.Subtract(cur
);
79 if (diff
.TotalSeconds
== 0)
88 if (diff
.Minutes
== 0)
90 return String
.Format("{0} second{1} ago",
91 diff
.Seconds
, diff
.Seconds
> 1 ? "s" : String
.Empty
);
95 return String
.Format("{0} minute{1} ago",
96 diff
.Minutes
, diff
.Minutes
> 1 ? "s" : String
.Empty
);
101 return String
.Format("{0} hour{1} ago",
102 diff
.Hours
, diff
.Hours
> 1 ? "s" : String
.Empty
);
107 return String
.Format("{0} day{1} ago",
108 diff
.Days
, diff
.Days
> 1 ? "s" : String
.Empty
);
113 /// Formats to short date
115 /// <param name="date"></param>
116 /// <returns>Short date, or <c>String.Empty</c> if <paramref name="date"/> is <c>null</c>.</returns>
117 public String
ToShortDate(DateTime
? date
)
119 return date
.HasValue
? date
.Value
.ToShortDateString() : string.Empty
;
123 /// Formats to short date
125 /// <param name="date"></param>
126 /// <returns>Short date and time, or <c>String.Empty</c> if <paramref name="date"/> is <c>null</c>.</returns>
127 public String
ToShortDateTime(DateTime
? date
)
129 return date
.HasValue
? date
.Value
.ToShortDateString() + " " + date
.Value
.ToShortTimeString() : string.Empty
;