How to get the Week number, First, and last day for the given date

Hi, UiPath forum

I need to get the week number of the year, the first , and last day date of the week for the given date.

can you please help me to get this?

Sample Input

Input = 18.10.2023 current week
Input = 21.10.2023 current week
Input = 25.10.2023 Next week

Output

W42_16.10.2023 - 22.10.2023
W43_23.10.2023 - 29.10.2023

Hi @K_GOWTHAM

This expression will get the Week Number in Integer

CInt(System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(New Datetime(2023,10,18),System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Sunday).ToString)

First Date of the week

New dateTime(Now.Year,1,1).AddDays(7*(CInt(System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(New Datetime(2023,10,18),System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Sunday).ToString)-1)-CInt(New dateTime(Now.Year,1,1).DayOfWeek)+1)

Last date of the Week

New dateTime(Now.Year,1,1).AddDays(7*(CInt(System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(New Datetime(2023,10,18),System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Sunday).ToString)-1)+7-CInt(New dateTime(Now.Year,1,1).DayOfWeek))

image

Kindly find the video link

Regards
Gokul

2 Likes

@K_GOWTHAM

  • input date:
 inputDate As Date = DateTime.ParseExact("25.10.2023", "dd.MM.yyyy", 
     CultureInfo.InvariantCulture)
  • The week number of the year
  weekNumber As Integer = Calendar.GetWeekOfYear(inputDate, 
    CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
  • Find the first day of the week (Monday)
  firstDayOfWeek As Date = inputDate.AddDays(DayOfWeek.Monday - 
   inputDate.DayOfWeek)
  • Find the last day of the week (Sunday)
    lastDayOfWeek As Date = firstDayOfWeek.AddDays(6)

  • Create the output string

outputString As String = String.Format("W{0}_{1:dd.MM.yyyy} - {2:dd.MM.yyyy}", 
   weekNumber, firstDayOfWeek, lastDayOfWeek)
  • Output the result
    WriteLine(outputString)
1 Like

@K_GOWTHAM

“W”+Cstr(Math.Round(Now.DayOfYear/7))+“_”+Now.AddDays(-CInt(Now.DayOfWeek)+1).ToString(“dd.MM.yyyy”)+“-”+Now.AddDays(CInt(Now.DayOfWeek)+1).ToString(“dd.MM.yyyy”)

output is “W42_16.10.2023_22.10.2023”

try this

1 Like

Hi,

How about the following?

varDate = DateTime.ParseExact(System.Text.RegularExpressions.Regex.Match(strData,"\b\d{2}.\d{2}.\d{4}").Value,"d.M.yyyy",System.Globalization.CultureInfo.InvariantCulture)

Then

varStartDate = varDate.AddDays(CInt(1-varDate.DayOfWeek))

varEndDate = varDate.AddDays(7-CInt(varDate.DayOfWeek))

weeknumber = System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(varDate,System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Sunday)

Sequence2.xaml (8.1 KB)

Regards,

1 Like

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