/// <summary>
/// Returns the daytime of the day of the week that the specified date is in.
/// </summary>
public static DateTime GetDayOfWeek(DateTime dayInWeek, DayOfWeek dayOfWeek)
{
DateTime lastDayInWeek = dayInWeek.Date;
int increment = 1;
if (dayOfWeek < dayInWeek.DayOfWeek) {
increment = -1;
}
while (lastDayInWeek.DayOfWeek != dayOfWeek)
{
lastDayInWeek = lastDayInWeek.AddDays(increment);
}
return lastDayInWeek;
}
implementation samples:
get the first day of the week:
DayOfWeek dayOfWeekPriority = DayOfWeek.Monday;
DateTime iniDatePriority = DateTime.Now;
DateTime endDatePriority =GetDayOfWeek(iniDatePriority, dayOfWeekPriority).AddDays(1);
get the last day of the week:
DayOfWeek dayOfWeekPriority = DayOfWeek.Friday;
DateTime iniDatePriority = DateTime.Now;
DateTime endDatePriority = DashboardManager.GetDayOfWeek(iniDatePriority, dayOfWeekPriority).AddDays(1);
Resource:
http://joelabrahamsson.com/getting-the-first-day-in-a-week-with-c/
/// Returns the daytime of the day of the week that the specified date is in.
/// </summary>
public static DateTime GetDayOfWeek(DateTime dayInWeek, DayOfWeek dayOfWeek)
{
DateTime lastDayInWeek = dayInWeek.Date;
int increment = 1;
if (dayOfWeek < dayInWeek.DayOfWeek) {
increment = -1;
}
while (lastDayInWeek.DayOfWeek != dayOfWeek)
{
lastDayInWeek = lastDayInWeek.AddDays(increment);
}
return lastDayInWeek;
}
implementation samples:
get the first day of the week:
DayOfWeek dayOfWeekPriority = DayOfWeek.Monday;
DateTime iniDatePriority = DateTime.Now;
DateTime endDatePriority =GetDayOfWeek(iniDatePriority, dayOfWeekPriority).AddDays(1);
get the last day of the week:
DayOfWeek dayOfWeekPriority = DayOfWeek.Friday;
DateTime iniDatePriority = DateTime.Now;
DateTime endDatePriority = DashboardManager.GetDayOfWeek(iniDatePriority, dayOfWeekPriority).AddDays(1);
Resource:
http://joelabrahamsson.com/getting-the-first-day-in-a-week-with-c/
1 comment:
Faster way : ttp://stackoverflow.com/a/22278311/3315914
Post a Comment