Dot Net Perls
C#

DateTime Year and Day

by Sam Allen

Problem

Find dates such as yesterday and tomorrow. You must find the date of the last and first days of any specified year. You want a usable DateTime built from any year.

Solution: C#

You are probably familiar with the DateTime static class. You cannot make a new DateTime with the new keyword, but that isn't a big problem. The first methods I show here find the previous and next days.

Example: find yesterday

Knowing yesterday is important for ASP.NET and Windows Forms projects, and many others. We must use the Add method on Today, but with a little twist--we add a negative number to the date. When you add a negative, you are actually subtracting.

/// <summary>
/// A static class that stores our DateTime helper methods.
/// </summary>
static class DateHelpers
{
    /// <summary>
    /// Method to find the previous day (yesterday) to the current day.
    /// </summary>
    /// <returns>The date for yesterday.</returns>
    static public DateTime GetYesterday()
    {
        // This is yesterday.
        DateTime yesterday = DateTime.Today.AddDays(-1);

        Console.WriteLine("Yesterday: " + yesterday.ToShortDateString());
        return yesterday;
    }
}

Example: find tomorrow and today

In the same vein as yesterday, I want to show tomorrow and today. More complex methods are coming up later, but some of the more basic ones are probably of help as well. These two methods are stored in the same class.

static class DateHelpers
{
    /// <summary>
    /// Example method to display the current day.
    /// </summary>
    static public void ShowToday()
    {
        // This is today.
        DateTime today = DateTime.Today;

        // Print out the result.
        Console.WriteLine("Today: " + today.ToShortDateString());
    }

    /// <summary>
    /// Finds the next day (tomorrow) from the current day.
    /// </summary>
    /// <returns>The date corresponding to tomorrow.</returns>
    static public DateTime GetTomorrow()
    {
        // This is tomorrow.
        DateTime tomorrow = DateTime.Today.AddDays(1);

        Console.WriteLine("Tomorrow: " + tomorrow.ToShortDateString());
        return tomorrow;
    }
}

Example: find first day in year

You may want to display a range of entries in your database-connected Windows or ASP.NET program. So you want to take a single day, and display all entries on that date. One way to do that is to use a database SQL query of a date range.

/// <summary>
/// Overloaded method to return the first day of the current year
/// (from today).
/// </summary>
/// <returns>The first day of the current year.</returns>
static public DateTime FirstDayOfYear()
{
    return FirstDayOfYear(DateTime.Today);
}

/// <summary>
/// Finds the first day of year in which the selected day
/// falls (as in the first day of that year).
/// </summary>
/// <param name="selectedDay">The day of the year you want
/// the first day of.</param>
/// <returns>The first day of the year.</returns>
static public DateTime FirstDayOfYear(DateTime selectedDay)
{
    // First day of the current year.
    DateTime firstDay = DateTime.Parse(selectedDay.Year.ToString() + "-01-01");

    Console.WriteLine("First of this year: " + firstDay.ToShortDateString());
    return firstDay;
}

Example: find last day in year

Due to Leap Year, there are not only 365 days in each year. This means you cannot assume that each year has the same number of days. The following methods may be useful if you need to count days. You could use different approaches here, but these work well.

/// <summary>
/// Finds the last day of the year for the current day (today).
/// </summary>
/// <returns>The last day of the current year.</returns>
static public DateTime LastDayOfYear()
{
    return LastDayOfYear(DateTime.Today);
}

/// <summary>
/// Finds the last day of the year for the selected day's year.
/// </summary>
/// <param name="selectedDay">The day that falls in the year.</param>
/// <returns>The last day of the year.</returns>
static public DateTime LastDayOfYear(DateTime selectedDay)
{
    // Find last day of the current year.
    // First, get the first day of the next year.
    DateTime firstNextYear = DateTime.Parse(
        (selectedDay.Year + 1).ToString() + "-01-01");
    DateTime lastDay = firstNextYear.AddDays(-1);

    Console.WriteLine("Last of this year: " + lastDay.ToShortDateString());
    return lastDay;
}

Tip: find time elapsed since date

You can easily find the "age" of a certain date, and how long ago it was in time. You can do this with DateTime.Subtract, and my article about elapsed days can help offer some tips.

Conclusion

This will literally help you get a date. I showed useful helpers for database queries and ranges. The complete class is available at my source code archive.

© 2008 Sam Allen. All rights reserved.

Ads by The Lounge