How to get the week number (With respect to month).See example in description

Hi

Please help me with the problem.

Problem:- I want to get the number of the current week of the month (not year).
Like if October starts from Monday, 9th October shall be in second week, 15 shall be the second week and so on.

I am sure it’s a silly doubt, but I would be glad if you could help.

Thanks and regards,
@hacky

1 Like

Here is the C# code -

static int GetWeekNumberOfMonth(DateTime date)
{
    date = date.Date;
    DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
    DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    if (firstMonthMonday > date)
    {
        firstMonthDay = firstMonthDay.AddMonths(-1);
        firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    }
    return (date - firstMonthMonday).Days / 7 + 1;
}

Test:

Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 1, 6)));  // 1
Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 1, 30))); // 4
Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 2, 1)));  // 4
Console.WriteLine(GetWeekNumberOfMonth(new DateTime(2014, 2, 3)));  // 1

Refer this post

// variable type of in_Today DateTime (this will be your input datetime)
// variable type of Basedate is DateTime
// variable type of week is Int64

Assign Basedate = new DateTime(in_Today.Year, in_Today.Month, 1)
Assign week = DateAndTime.DateDiff(DateInterval.WeekOfYear ,Basedate, in_Today, FirstDayOfWeek.Monday,FirstWeekOfYear.Jan1)
actual value = week+1

@Karthick_Settu, can you please provide me with the simple flow? since I am not able to do this.

@KarthikByggari,

This C# code looks perfectly fine. But I might need the simple flow demonstrating the same.

Kindly help.

Check this file.
Main.xaml (7.1 KB)

10 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.