Introduction
If you want to insert the start of today into a database, you need to pass today's date, at midnight. That isn't difficult, but the code looks clumsy, and needs to be commented:
using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable VALUES (@DATE)", con))
{
DateTime now = DateTime.Now;
cmd.Parameters.AddWithValue("@DATE", new DateTime(now.Year, now.Month, now.Day));
cmd.ExecuteNonQuery();
}
It would be much easier to read if you could just say:
using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable VALUES (@DATE)", con))
{
cmd.Parameters.AddWithValue("@DATE", DateTime.Now.AtMidnight());
cmd.ExecuteNonQuery();
}
In addition, there are times when you want to know the date next Tuesday, or the first Monday of a particular month.
This tip presents a small number of extension methods to do just that.
Using the Code
Add a new class file to your project, and copy the code below into it.
You can then use the extension methods as normal:
DateTime pickedDate = dateTimePicker1.Value;
Console.WriteLine("Start date : {0}", pickedDate);
Console.WriteLine("First of month : {0}", pickedDate.FirstOfMonth());
Console.WriteLine("First Tuesday of month : {0}", pickedDate.FirstOfMonth(DayOfWeek.Tuesday));
Console.WriteLine("Last of month : {0}", pickedDate.LastOfMonth());
Console.WriteLine("Last Tuesday of month : {0}", pickedDate.LastOfMonth(DayOfWeek.Tuesday));
Console.WriteLine("Next Tuesday : {0}", pickedDate.NextDayOfWeek(DayOfWeek.Tuesday));
Console.WriteLine("Start of day : {0}", pickedDate.AtMidnight());
Console.WriteLine("Midday : {0}", pickedDate.AtMidday());
The Method Code
using System;
namespace FieldFilterTest
{
public static class DateTimeExtensions
{
#region Public Methods
public static DateTime FirstOfMonth(this DateTime dt)
{
return (dt.AddDays(1 - dt.Day)).AtMidnight();
}
public static DateTime FirstOfMonth(this DateTime dt, DayOfWeek dayOfWeek)
{
DateTime firstDayOfMonth = dt.FirstOfMonth();
return (firstDayOfMonth.DayOfWeek == dayOfWeek ? firstDayOfMonth :
firstDayOfMonth.NextDayOfWeek(dayOfWeek)).AtMidnight();
}
public static DateTime LastOfMonth(this DateTime dt)
{
int daysInMonth = DateTime.DaysInMonth(dt.Year, dt.Month);
return dt.FirstOfMonth().AddDays(daysInMonth - 1).AtMidnight();
}
public static DateTime LastOfMonth(this DateTime dt, DayOfWeek dayOfWeek)
{
DateTime lastDayOfMonth = dt.LastOfMonth();
return lastDayOfMonth.AddDays(lastDayOfMonth.DayOfWeek < dayOfWeek ?
dayOfWeek - lastDayOfMonth.DayOfWeek - 7 :
dayOfWeek - lastDayOfMonth.DayOfWeek) ;
}
public static DateTime NextDayOfWeek(this DateTime dt, DayOfWeek dayOfWeek)
{
int offsetDays = dayOfWeek - dt.DayOfWeek;
return dt.AddDays(offsetDays > 0 ? offsetDays : offsetDays + 7).AtMidnight();
}
public static DateTime AtMidnight(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
}
public static DateTime AtMidday(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, dt.Day, 12, 0, 0);
}
#endregion
}
}
History
- V1.1 Added missing
LastOfMonth
method. Still not sure where it went... - V1.2 6th June 2012
LastOfMonth
fixed: was giving incorrect result when the sought dayOfWeek
enumeration value is more than the lastDayOfMonth.DayOfWeek
enumeration value.
Bug found and suggested fix by VJ Reddy[^] - My thanks to him, and my apologies for any inconvenience caused.
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?