Yusril Maulidan Raji
Yusril Maulidan Raji

Reputation: 1932

How get the instance of the added days of DateTimeFormatInfo.FirstDayOfWeek?

Here is my simplified code:

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;

var firstReference = dfi.FirstDayOfWeek; // Currently, this could be Monday, Saturday, or Sunday.
var secondReference = dfi.FirstDayOfWeek.AddDays(3); // For this one, it should be something like 3 days after the dynamic value of FirstDayOfWeek.

Unfortunately, it doesn't work like so. So, the question: is there any way to get the instance of the 3 days added dfi.FirstDayOfWeek ?

Upvotes: 2

Views: 158

Answers (3)

levent
levent

Reputation: 3634

you can iterate days on dayofweek..

        DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
        List<DayOfWeek> daylist = new List<DayOfWeek>();
        int i = (int)dfi.FirstDayOfWeek;
        int addDay = 3;
        while (i <= ((int)dfi.FirstDayOfWeek + addDay) % 7)
        {
            daylist.Add((DayOfWeek)i);
            i++;
        }

Upvotes: 0

Maksim Simkin
Maksim Simkin

Reputation: 9679

DayOfWeek is an Enum, so you could just cast it to int, add days and cast back, something like:

var secondReference = (DayOfWeek)(((int)dfi.FirstDayOfWeek+3)%7);

You need mod to avoid having DayofWeek greater than 7.

Upvotes: 5

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

Use

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;

var firstReference = dfi.FirstDayOfWeek;
var secondReference = (DayOfWeek)(((int)firstReference + 3) % 7);

DayOfWeek is Enum so addition works fine on it.

Used Mod in case where + 3 exceeds limits of DayOfWeek (Saturday in your case).

enter image description here

Upvotes: 2

Related Questions