Skip to content

Instantly share code, notes, and snippets.

@Oblongmana
Created April 28, 2013 23:12
Show Gist options
  • Save Oblongmana/5478812 to your computer and use it in GitHub Desktop.
Save Oblongmana/5478812 to your computer and use it in GitHub Desktop.
This contains #Extension methods for #DateTime objects. The method WeekSpan gives you Tuple<DateTime,DateTime? representing a week from the start of the day a week ago to the end of the DateTime instance calling the method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyExtensions
{
public static class DateTimeExtensions
{
/// <summary>
/// Returns a tuple representing a week span (StartOfWeek,EndOfWeek), given a particular date (which is the last day of the week).
/// For example, given Tuesday 12:01pm, will return (PrevWednesday 00:00:00:000, Tuesday 23:59:59:999)
/// </summary>
/// <param name="endOfWeek"></param>
/// <returns></returns>
public static Tuple<DateTime, DateTime> WeekSpan(this DateTime endOfWeek)
{
DateTime weekEnded = new DateTime(endOfWeek.Year, endOfWeek.Month, endOfWeek.Day, 23, 59, 59,999);
DateTime weekStarted = weekEnded.AddDays(-7).AddMilliseconds(1);
return new Tuple<DateTime, DateTime>(weekStarted, weekEnded);
}
public static DateTime StartOfDay(this DateTime theDateTime)
{
return new DateTime(theDateTime.Year, theDateTime.Month, theDateTime.Day, 0, 0, 0, 0);
}
public static DateTime EndOfDay(this DateTime theDateTime)
{
return new DateTime(theDateTime.Year, theDateTime.Month, theDateTime.Day, 23, 59, 59, 999);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment