Contact

admin

About Me · Send mail to the author(s) E-mail · Twitter

At GROSSWEBER we practice what we preach. We offer trainings for modern software technologies like Behavior Driven Development, Clean Code and Git. Our staff is fluent in a variety of languages, including English.

Feed Icon

Tags

Open Source Projects

Archives

Blogs of friends

Now playing [?]

Error retrieving information from external service.
Audioscrobbler/Last.fm

ClustrMap

Getting the Next Week's First Day in .NET

Posted in Geek Mode at Wednesday, August 16, 2006 8:30 PM W. Europe Daylight Time

Here's a code snippet I've written last year for a test. I don't remember of what use it was to me, I found it today while removing clutter from my local project repository.

This piece of code iterates through all days of the current month and then prints the first day of the next week in the invariant culture (where Sunday is the first day of the week) and the German culture (Monday is the first day of the week).

using System;
using System.Threading;
using System.Globalization;
 
class Program
{
    static void Main(string[] args)
    {
        // The first day of the week is Sunday.
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        RunTest();
 
        // The first day of the week is Monday.
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-de");
        RunTest();
    }
 
    /// <summary>
    /// Iterates through all days of the current month and prints the 
    /// first day of the next week according to the current culture.
    /// </summary>
    private static void RunTest()
    {
        Console.WriteLine("First day of week is {0}",
            Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
 
        // Iterate through all dates of the current month.
        DateTime date = DateTime.Today.AddDays(-DateTime.Today.Day);
        for (int i = 0; i < DateTime.DaysInMonth(date.Year, date.Month); i++)
        {
            date = date.AddDays(1);
 
            DateTime nextWeekFirst = GetNextWeeksFirstDay(date);
            Console.WriteLine("{0} -> {1} ({2})",
                date.ToShortDateString(),
                nextWeekFirst.ToShortDateString(),
                nextWeekFirst.DayOfWeek);
        }
    }
 
    /// <summary>
    /// Returns the date of the next week's first day for a given <see cref="DateTime"/>.
    /// </summary>
    private static DateTime GetNextWeeksFirstDay(DateTime date)
    {
        int daysUntilNextWeeksFirstDay =
            Convert.ToInt32(Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek) -
            Convert.ToInt32(date.DayOfWeek) + 7;
 
        if (daysUntilNextWeeksFirstDay == 8)
        {
            daysUntilNextWeeksFirstDay = 1;
        }
 
        return date.AddDays(daysUntilNextWeeksFirstDay);
    }
}

Output (shortened):

First day of week is Sunday
08/01/2006 -> 08/06/2006 (Sunday)
08/02/2006 -> 08/06/2006 (Sunday)
08/03/2006 -> 08/06/2006 (Sunday)
08/04/2006 -> 08/06/2006 (Sunday)
08/05/2006 -> 08/06/2006 (Sunday)
08/06/2006 -> 08/13/2006 (Sunday)
08/07/2006 -> 08/13/2006 (Sunday)
...
First day of week is Monday
01.08.2006 -> 07.08.2006 (Monday)
02.08.2006 -> 07.08.2006 (Monday)
03.08.2006 -> 07.08.2006 (Monday)
04.08.2006 -> 07.08.2006 (Monday)
05.08.2006 -> 07.08.2006 (Monday)
06.08.2006 -> 07.08.2006 (Monday)
07.08.2006 -> 14.08.2006 (Monday)
08.08.2006 -> 14.08.2006 (Monday)
...
Tuesday, November 10, 2009 10:39:12 AM (W. Europe Standard Time, UTC+01:00)
Thanks, very helpful article.
Kefren
Saturday, June 12, 2010 8:40:50 AM (W. Europe Daylight Time, UTC+02:00)
Thanks. Quite helpful.
Shrek
All comments require the approval of the site owner before being displayed.
(will show your gravatar icon)
 
[Captcha]Enter the code shown (prevents robots):

Live Comment Preview