System.DateTime Extension Methods

EndOfDay()

Summary: This method returns the Date portion of the specified DateTime with the Time set to "23:59:59". The last moment of the day.
*C# Example:"
var dt = DateTime.Now.EndOfDay();

EndOfMonth()

Summary: This method returns the Date of the last day in the same Month as the specified DateTime, with the Time set to "23:59:59"
var dt = DateTime.Now.EndOfMonth();

EndOfWeek(startDayOfWeek)

Summary: Returns the "EndOfDay" for the last day in the week of the specified DateTime. If startDayOfWeek is not specified then DayOfWeek.Sunday is used by default.
C# Example:
var dt = DateTime.Now;
var start = dt.EndOfWeek(DayOfWeek.Monday);

EndOfYear()

Summary: Returns the Date of the last day in the same year as the specified DateTime, with the Time set to "23:59:59"
C# Example:
var dt = DateTime.Now.EndOfYear();

IsWeekDay()

*Summary:" This method returns whether the DateTime is on a Week Day.
C# Example:
DateTime dt;
if (dt.IsWeekDay())
{
    // do something
}

IsWeekend()

Summary: This method returns whether the DateTime is on a Weekend.
C# Example:
DateTime dt;
if (dt.IsWeekend())
{
    // do something
}

MillisecondsSince1970()

Summary: Returns the number of milliseconds from January 1st, 1970 until the specified DateTime
C# Example:
var since1970 = DateTime.Now.MillisecondsSince1970();

RoundToNearest(minutes)

Summary: Returns the DateTime rounded to the nearest specified number of minutes
C# Example:
// Round to the nearest 15 minutes
var dt = DateTime.Now.RoundToNearest(15);

RoundToNearest(timeSpan)

Summary: Returns the DateTime rounded to the nearest specified TimeSpan interval
C# Example:
// using a TimeSpan
var timespan = new TimeSpan(0, 15, 0);
var dt = DateTime.Now.RoundToNearest(timespan);

// using Int.Minutes extension method to create the TimeSpan
var dt = DateTime.Now.RoundToNearest(15.Minutes());

StartOfDay()

Summary: This method returns the Date portion of the specified DateTime with the Time set to "00:00:00". The first moment of the day.
C# Example:
var dt = DateTime.Now.StartOfDay();

StartOfMonth()

Summary: This method returns the Date of the first day in the same Month as the specified DateTime, with the Time set to "00:00:00"
var dt = DateTime.Now.StartOfMonth();

StartOfWeek(startDayOfWeek)

Summary: Returns the "StartOfDay" for the first day in the week of the specified DateTime. If startDayOfWeek is not specified then DayOfWeek.Sunday is used by default.
C# Example:
var dt = DateTime.Now;
var start = dt.StartOfWeek(DayOfWeek.Monday);

StartOfYear()

Summary: Returns the Date of the first day in the same year as the specified DateTime, with the Time set to "00:00:00"
C# Example:
var dt = DateTime.Now.StartOfYear();

Last edited May 4, 2012 at 2:06 PM by crpietschmann, version 12

Comments

No comments yet.