Date difference showing error

Hello everyone i want to find difference of two dates but i am getting an error. can anyone can guide me how to resolve it.

Conversion from string “27-07-2023” to type ‘Date’ is not valid.
Kindly go through the attached image.

@Ashish_Tyagi show me how you are converting from string to date

Please refer image

@Ashish_Tyagi
Show me your both date format.

@Ashish_Tyagi

Instead od cdate use this

Datetime.ParseExact(datevar,"dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture)

If the format is different then change this accordingly dd-MM-yyyy

Cheers

Format for both the dates are same. dd-MM-yyyy

image

image

1 Like

@Ashish_Tyagi
Try this

Cint(DateDiff(DateInterval.Day, Datetime.ParseExact(Today_date,“dd-MM-yyyy”,System.Globalization.CultureInfo.InvariantCulture),Datetime.ParseExact(Invoice_Date,“dd-MM-yyyy”,System.Globalization.CultureInfo.InvariantCulture)))
Try this one

@Ashish_Tyagi Ok try above one.
Replace inverted Commas with this "

@raja.arslankhan
It is showing the below error.

image

@Ashish_Tyagi

Cint(DateDiff(DateInterval.Day,Datetime.ParseExact(Today_Date,"dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture),Datetime.ParseExact(Invoice_Date,"dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture)))

Please use the above provided one…and this error is because of wrong inverted comma…change them in code

correct: "
wrong: ”

cheers

@Ashish_Tyagi
Try to convert inverted Commas “” as Anil mentioned. When we write here it changes to wrong one.

Cint(DateDiff(DateInterval.Day,Datetime.ParseExact(Today_Date,"dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture),Datetime.ParseExact(Invoice_Date,"dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture)))

@Anil_G
I got that earlier. Can you please explain the reason why it was not working using Cdate?

@Anil_G Because your date format contains dash format like “dd-MM-yyyy”

@Ashish_Tyagi
If your problem solved then mark it as a solution so that others can get help from it. Thanks

@Ashish_Tyagi

Cdate by default converts the date which is in your system date format only…any other formats need to be converted using parseexact method…

hope this helps

Cheers

@Anil_G
Thanks.

1 Like

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