comparison MetroWpf/MetroWpf.Framework/Extensions/DateTimeExtensions.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 using System;
2 using System.Globalization;
3
4 namespace MetroWpf
5 {
6 /// <summary>
7 /// Extension methods for the DateTimeOffset data type.
8 /// </summary>
9 public static class DateTimeExtensions
10 {
11 #region · Extensions ·
12
13 /// <summary>
14 /// Calculates the age based on today.
15 /// </summary>
16 /// <param name="dateOfBirth">The date of birth.</param>
17 /// <returns>The calculated age.</returns>
18 public static int CalculateAge(this DateTime dateOfBirth)
19 {
20 return CalculateAge(dateOfBirth, DateTime.Today);
21 }
22
23 /// <summary>
24 /// Calculates the age based on a passed reference date.
25 /// </summary>
26 /// <param name="dateOfBirth">The date of birth.</param>
27 /// <param name="referenceDate">The reference date to calculate on.</param>
28 /// <returns>The calculated age.</returns>
29 public static int CalculateAge(this DateTime dateOfBirth, DateTime referenceDate)
30 {
31 int years = referenceDate.Year - dateOfBirth.Year;
32
33 if (referenceDate.Month < dateOfBirth.Month
34 || (referenceDate.Month == dateOfBirth.Month
35 && referenceDate.Day < dateOfBirth.Day))
36 {
37 --years;
38 }
39
40 return years;
41 }
42
43 /// <summary>
44 /// Returns the number of days in the month of the provided date.
45 /// </summary>
46 /// <param name="date">The date.</param>
47 /// <returns>The number of days.</returns>
48 public static int GetCountDaysOfMonth(this DateTime date)
49 {
50 var nextMonth = date.AddMonths(1);
51
52 return new DateTime(nextMonth.Year, nextMonth.Month, 1).AddDays(-1).Day;
53 }
54
55 /// <summary>
56 /// Returns the first day of the month of the provided date.
57 /// </summary>
58 /// <param name="date">The date.</param>
59 /// <returns>The first day of the month</returns>
60 public static DateTime GetFirstDayOfMonth(this DateTime date)
61 {
62 return new DateTime(date.Year, date.Month, 1);
63 }
64
65 /// <summary>
66 /// Returns the first day of the month of the provided date.
67 /// </summary>
68 /// <param name="date">The date.</param>
69 /// <param name="dayOfWeek">The desired day of week.</param>
70 /// <returns>The first day of the month</returns>
71 public static DateTime GetFirstDayOfMonth(this DateTime date, DayOfWeek dayOfWeek)
72 {
73 var dt = date.GetFirstDayOfMonth();
74
75 while (dt.DayOfWeek != dayOfWeek)
76 {
77 dt = dt.AddDays(1);
78 }
79
80 return dt;
81 }
82
83 /// <summary>
84 /// Returns the last day of the month of the provided date.
85 /// </summary>
86 /// <param name="date">The date.</param>
87 /// <returns>The last day of the month.</returns>
88 public static DateTime GetLastDayOfMonth(this DateTime date)
89 {
90 return new DateTime(date.Year, date.Month, GetCountDaysOfMonth(date));
91 }
92
93 /// <summary>
94 /// Returns the last day of the month of the provided date.
95 /// </summary>
96 /// <param name="date">The date.</param>
97 /// <param name="dayOfWeek">The desired day of week.</param>
98 /// <returns>The date time</returns>
99 public static DateTime GetLastDayOfMonth(this DateTime date, DayOfWeek dayOfWeek)
100 {
101 var dt = date.GetLastDayOfMonth();
102
103 while (dt.DayOfWeek != dayOfWeek)
104 {
105 dt = dt.AddDays(-1);
106 }
107
108 return dt;
109 }
110
111 /// <summary>
112 /// Indicates whether the date is today.
113 /// </summary>
114 /// <param name="dt">The date.</param>
115 /// <returns>
116 /// <c>true</c> if the specified date is today; otherwise, <c>false</c>.
117 /// </returns>
118 public static bool IsToday(this DateTime dt)
119 {
120 return (dt.Date == DateTime.Today);
121 }
122
123 /// <summary>
124 /// Sets the time on the specified DateTime value.
125 /// </summary>
126 /// <param name="date">The base date.</param>
127 /// <param name="hours">The hours to be set.</param>
128 /// <param name="minutes">The minutes to be set.</param>
129 /// <param name="seconds">The seconds to be set.</param>
130 /// <returns>The DateTime including the new time value</returns>
131 public static DateTime SetTime(this DateTime date, int hours, int minutes, int seconds)
132 {
133 return date.SetTime(new TimeSpan(hours, minutes, seconds));
134 }
135
136 /// <summary>
137 /// Sets the time on the specified DateTime value.
138 /// </summary>
139 /// <param name="date">The base date.</param>
140 /// <param name="time">The TimeSpan to be applied.</param>
141 /// <returns>
142 /// The DateTime including the new time value
143 /// </returns>
144 public static DateTime SetTime(this DateTime date, TimeSpan time)
145 {
146 return date.Date.Add(time);
147 }
148
149 /// <summary>
150 /// Converts a DateTime into a DateTimeOffset using the local system time zone.
151 /// </summary>
152 /// <param name="localDateTime">The local DateTime.</param>
153 /// <returns>The converted DateTimeOffset</returns>
154 public static DateTimeOffset ToDateTimeOffset(this DateTime localDateTime)
155 {
156 return localDateTime.ToDateTimeOffset(null);
157 }
158
159 /// <summary>
160 /// Converts a DateTime into a DateTimeOffset using the specified time zone.
161 /// </summary>
162 /// <param name="localDateTime">The local DateTime.</param>
163 /// <param name="localTimeZone">The local time zone.</param>
164 /// <returns>The converted DateTimeOffset</returns>
165 public static DateTimeOffset ToDateTimeOffset(this DateTime localDateTime, TimeZoneInfo localTimeZone)
166 {
167 localTimeZone = (localTimeZone ?? TimeZoneInfo.Local);
168
169 if (localDateTime.Kind != DateTimeKind.Unspecified)
170 {
171 localDateTime = new DateTime(localDateTime.Ticks, DateTimeKind.Unspecified);
172 }
173
174 return TimeZoneInfo.ConvertTimeToUtc(localDateTime, localTimeZone);
175 }
176
177 /// <summary>
178 /// Gets the first day of the week using the current culture.
179 /// </summary>
180 /// <param name="date">The date.</param>
181 /// <returns>The first day of the week</returns>
182 public static DateTime GetFirstDayOfWeek(this DateTime date)
183 {
184 return date.GetFirstDayOfWeek(null);
185 }
186
187 /// <summary>
188 /// Gets the first day of the week using the specified culture.
189 /// </summary>
190 /// <param name="date">The date.</param>
191 /// <param name="cultureInfo">The culture to determine the first weekday of a week.</param>
192 /// <returns>The first day of the week</returns>
193 public static DateTime GetFirstDayOfWeek(this DateTime date, CultureInfo cultureInfo)
194 {
195 cultureInfo = (cultureInfo ?? CultureInfo.CurrentCulture);
196
197 var firstDayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;
198
199 while (date.DayOfWeek != firstDayOfWeek)
200 {
201 date = date.AddDays(-1);
202 }
203
204 return date;
205 }
206
207 /// <summary>
208 /// Gets the last day of the week using the current culture.
209 /// </summary>
210 /// <param name="date">The date.</param>
211 /// <returns>The first day of the week</returns>
212 public static DateTime GetLastDayOfWeek(this DateTime date)
213 {
214 return date.GetLastDayOfWeek(null);
215 }
216
217 /// <summary>
218 /// Gets the last day of the week using the specified culture.
219 /// </summary>
220 /// <param name="date">The date.</param>
221 /// <param name="cultureInfo">The culture to determine the first weekday of a week.</param>
222 /// <returns>The first day of the week</returns>
223 public static DateTime GetLastDayOfWeek(this DateTime date, CultureInfo cultureInfo)
224 {
225 return date.GetFirstDayOfWeek(cultureInfo).AddDays(6);
226 }
227
228 /// <summary>
229 /// Gets the next occurence of the specified weekday within the current week using the current culture.
230 /// </summary>
231 /// <param name="date">The base date.</param>
232 /// <param name="weekday">The desired weekday.</param>
233 /// <returns>The calculated date.</returns>
234 /// <example><code>
235 /// var thisWeeksMonday = DateTime.Now.GetWeekday(DayOfWeek.Monday);
236 /// </code></example>
237 public static DateTime GetWeeksWeekday(this DateTime date, DayOfWeek weekday)
238 {
239 return date.GetWeeksWeekday(weekday, null);
240 }
241
242 /// <summary>
243 /// Gets the next occurence of the specified weekday within the current week using the specified culture.
244 /// </summary>
245 /// <param name="date">The base date.</param>
246 /// <param name="weekday">The desired weekday.</param>
247 /// <param name="cultureInfo">The culture to determine the first weekday of a week.</param>
248 /// <returns>The calculated date.</returns>
249 /// <example><code>
250 /// var thisWeeksMonday = DateTime.Now.GetWeekday(DayOfWeek.Monday);
251 /// </code></example>
252 public static DateTime GetWeeksWeekday(this DateTime date, DayOfWeek weekday, CultureInfo cultureInfo)
253 {
254 var firstDayOfWeek = date.GetFirstDayOfWeek(cultureInfo);
255
256 return firstDayOfWeek.GetNextWeekday(weekday);
257 }
258
259 /// <summary>
260 /// Gets the next occurence of the specified weekday.
261 /// </summary>
262 /// <param name="date">The base date.</param>
263 /// <param name="weekday">The desired weekday.</param>
264 /// <returns>The calculated date.</returns>
265 /// <example><code>
266 /// var lastMonday = DateTime.Now.GetNextWeekday(DayOfWeek.Monday);
267 /// </code></example>
268 public static DateTime GetNextWeekday(this DateTime date, DayOfWeek weekday)
269 {
270 while (date.DayOfWeek != weekday)
271 {
272 date = date.AddDays(1);
273 }
274
275 return date;
276 }
277
278 /// <summary>
279 /// Gets the previous occurence of the specified weekday.
280 /// </summary>
281 /// <param name="date">The base date.</param>
282 /// <param name="weekday">The desired weekday.</param>
283 /// <returns>The calculated date.</returns>
284 /// <example><code>
285 /// var lastMonday = DateTime.Now.GetPreviousWeekday(DayOfWeek.Monday);
286 /// </code></example>
287 public static DateTime GetPreviousWeekday(this DateTime date, DayOfWeek weekday)
288 {
289 while (date.DayOfWeek != weekday)
290 {
291 date = date.AddDays(-1);
292 }
293
294 return date;
295 }
296
297 #endregion
298 }
299 }