How to find week No for given data format

Hii Team ,

Have used below approach to find week no

Example

inputdata = “202428”

Input data contains the date which means in this example it is year 2024, month is 2 and date is 8th

WeekNo = CInt(math.Round(datetime.ParseExact(inputdata,{“yyyyMMdd”,“yyyyMd”} ,system.Globalization.CultureInfo.InvariantCulture).DayOfYear/7)+1)

But week no is giving error in this case because inputdata is not present in this format 20230120

Could someone guide in finding week no for the input data in the given format

Thanks team in advance

Hi @NISHITHA

Try this:

weekNo = CInt(Math.Ceiling(DateTime.ParseExact(inputData.Insert(4, "-").Insert(6, "-"), "yyyy-M-dd", CultureInfo.InvariantCulture).DayOfYear / 7.0))

Hope it helps!!

Hi @NISHITHA my approach to this was to first check the inputdata length as there is no guarantee that you will not receive a date like 202422 (Feb 2, 2024). If the length was 8 chars then its good to go. When it is 7 then use string manipulation to get the first 4 chars as the year, the 5th as the month, and the 6th and 7th as the day. If it is 6 then first 4 as the year, 5th is month and 6th is day. You get the idea… The challenge when it is 7 in length is then how to decide which is the month and day for ambiguous input like 2024111 (Jan 11 or Nov 1?) or 2024121 (Dec 1 or Jan 21?)

Any other approach that could suggest

1 Like

Hi @NISHITHA

What approach do you want?

Regards

@Parvathy thanks for reference but its giving exception

What are the exception you are getting @NISHITHA

Regards

@Parvathy am getting System.InvalidOperation Expression

For this date

inputdata = “202428”

@NISHITHA

weekNo = (New System.Globalization.CultureInfo("en-US")).Calendar.GetWeekOfYear(DateTime.ParseExact(inputData, {"yyyyMMdd", "yyyyMd"}, System.Globalization.CultureInfo.InvariantCulture), System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)


whatever formats you have please mention in {“yyyyMMdd”, “yyyyMd”}

Hi @NISHITHA

Try this:

CInt(Math.Ceiling(DateTime.ParseExact("202428".Insert(4, "-").Insert(6, "-"), "yyyy-M-d", System.Globalization.CultureInfo.InvariantCulture).DayOfYear / 7.0))

Hope it helps!!

Parsing the datestring:
grafik

in the first case we get an exception as the pattern Mdd is ambiguous for the parser

In such cases we could think about fixing such dates with a RegeEx


But still can have some risks on those dates
yyyymd - 202412 - 2nd Jan 2024
yyyymd - 2024122 - which could be the 22th Jan 2024 or 2nd Dec 2024

Similar for a 6-digit length string we can do:
grafik
insert a 0 before and 0 afterwards

About: Weekno


We suggest to import System.Globalization to the namespaces and shortening the statement

A strategy could be to integrate within the flow a sorting out of all ambiguous dates and routing it back for manually inspections and corrections. But here we have to know more about the use case in general

1 Like

Thanks @Parvathy

This is working for the given scenario

1 Like

Thanks @ppr your right about the Observation

Will need to make changes

Hi @NISHITHA

If you find the solution for your query please mark my post as solution to close the loop.

Regards

Hi NISHITHA,
please give proper input value . then only we can able to calculate that the date format

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