Check if date falls within a certain date range disregarding the year

Hi all,
I have a datatable that consists of a column ‘Payment Date’ with date datatype. How can I check if this Payment Date (convert to MM/dd) and validate if it falls within this range >= 08/20 and <= 02/19 disregarding the year.
I would need this to construct in a if condition within for each row in datatable. I will assign a value if it falls within the date range stated above

I tried this but it’s not working with error message “string not recognized as valid datetime”. What am I doing wrong?

DateTime.ParseExact(row.item(“Payment Date”).ToString, “MM/dd”, System.Globalization.CultureInfo.InvariantCulture) >= DateTime.ParseExact(“08/20”, “MM/dd”, System.Globalization.CultureInfo.InvariantCulture) and DateTime.ParseExact(row.item(“Payment Date”).ToString, “MM/dd”, System.Globalization.CultureInfo.InvariantCulture) <= DateTime.ParseExact(“02/19”, “MM/dd”, System.Globalization.CultureInfo.InvariantCulture)

1 Like

Hi @niro ,
you can read file to get data table
check each row
for each row in data
dt.row(“‘Payment Date’”)

fine is 16/08/2023 is of format dd/MM/yyyy or MM/dd/yyyy
if its dd/MM/yyyy
then out_date = Datetime.ParseExact(“16/08/2023”,“dd/MM”,System.Globalization.CultureInfo.InvariantCulture)
where out_date is a variable of type datetime

or if its of format “MM/dd”
out_date = Datetime.ParseExact(“16/08/2023”,“MM/dd”,System.Globalization.CultureInfo.InvariantCulture)

then check between 08/20 and 02/19
but, you can share your sample format at “‘Payment Date” format ?
I will test it

Ok I will test it


For Each Row in DataTable
paymentDate = DateTime.ParseExact(row(“Payment Date”).ToString, “MM/dd/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).ToString(“MM/dd”)

lowerBound = DateTime.ParseExact("08/20", "MM/dd", System.Globalization.CultureInfo.InvariantCulture)
upperBound = DateTime.ParseExact("02/19", "MM/dd", System.Globalization.CultureInfo.InvariantCulture)

If DateTime.ParseExact(paymentDate, "MM/dd", System.Globalization.CultureInfo.InvariantCulture) >= lowerBound AndAlso DateTime.ParseExact(paymentDate, "MM/dd", System.Globalization.CultureInfo.InvariantCulture) <= upperBound Then
    row("Result") = "Within range"
    row("Result") = "Outside range"

Hi @niro

For Each row In yourDataTable.Rows
paymentDate As DateTime = CDate(row("Payment Date").ToString())
formattedDate As String = paymentDate.ToString("MM/dd")
    If (formattedDate >= "08/20" AndAlso formattedDate <= "02/19") Then
        row("New Column") = "Value to Assign"
    End If

Hope it helps!!

thank you @rigandu and @Parvathy both solutions are working

1 Like

You’re welvome @niro

Happy Automation

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