Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop week based on culture info

How do I loop the whole week (monday-sunday) based on culture info, so in my case monday will be the first day of the week? And is it possible to find the int value of the day at the same time?

For some information: I need to make this in order to make some generel opening hours for a store.

like image 324
janhartmann Avatar asked Feb 24 '23 05:02

janhartmann


1 Answers

I think what you need is the following loop.

        DayOfWeek firstDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
        for (int dayIndex = 0; dayIndex < 7; dayIndex++)
        {
            var currentDay = (DayOfWeek) (((int) firstDay + dayIndex) % 7);

            // Output the day
            Console.WriteLine(dayIndex + " " + currentDay);
        }

The modulo 7 is important, because the firstdayofweek can vary by different cultures.

like image 124
BitKFu Avatar answered Mar 08 '23 02:03

BitKFu