Getting wrong Last Week number

Getting wrong week number for the below code.

Expected week numberis 52 but the output says 53.

System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(Today.AddDays(-7),System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Monday)

@Sathish_Kumar_S

System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(Today.AddDays(-7), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

Hi @Sathish_Kumar_S

Assign datetime today=datetime.now
System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(today.AddDays(-7),System.Globalization.CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday)

Hi @Sathish_Kumar_S

Try this

System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(Today.AddDays(-7), System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)

Cheers!!

Hi @Sathish_Kumar_S

Output= new System.Globalization.CultureInfo("en-US").Calendar.GetWeekOfYear(DateTime.Now.AddDays(-7), System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
Message Box-> Output

Output is of DataType System.Int32

Check the below Image for better understanding:

Hope it helps!!
Regards
`

Hi,

If the expected week number is 52 but the output is 53, it might be due to how the week is defined in the specific calendar system. Some cultures and systems define the first week of the year differently.

You can try using different CalendarWeekRule and FirstDay combinations to see if it gives you the expected result.

you can try using CalendarWeekRule.FirstFourDayWeek or CalendarWeekRule.FirstFullWeek. as following

weekNumber = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(
Today.AddDays(-7),
System.Globalization.CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday
)

OR

weekNumber = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(
Today.AddDays(-7),
System.Globalization.CalendarWeekRule.FirstFullWeek,
DayOfWeek.Monday
)