Compare Dates to check if it falls within within specific range

Hi All,

I have two dates (M/dd/yyyy) format and date variables are in string
str_signed_date = 8/12/2024
str_start_date = 1/8/2025

I need to compare and see if str_start_date falls within 15 days range of str_signed_date.

I tried with below but not getting accurate answer .

@dutta.marina,

Try this logic.

str_signed_date = "8/12/2024"
str_start_date = "1/8/2025"

signedDate = DateTime.ParseExact(str_signed_date, "M/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
startDate = DateTime.ParseExact(str_start_date, "M/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

rangeStart = signedDate.AddDays(-15)
rangeEnd = signedDate.AddDays(15)

If startDate >= rangeStart AndAlso startDate <= rangeEnd
    // str_start_date falls within 15-day range of str_signed_date
Else
    // str_start_date does not fall within 15-day range of str_signed_date
End If

LLM helped me to write this but I think this should work.

@dutta.marina

Can you tell where is it failing?ideally that should give you the difference properly…unless dateparsing gave different date items

Cheers

@Anil_G

I was getting in negative result sometimes.

@dutta.marina

then you can use Math.abs(Expression)

it depends on start and end values

if signed date is after the other date or so

cheers

1 Like

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