Days difference

Number of days = DateDiff(DateInterval.Day, convert.ToDateTime(date_ETADate), convert.ToDateTime(date_ATADate))

date_ETADate = 03/09/2023 (MM/dd/yyyy)
date_ATADate = 15-Mar-2023 (dd-MMM-yyyy)

output receive = Number of days = -172days
but require output is 6 days.

Can someone help me to get this output.

Hi,

Pls try below one:

Number_of_days = Convert.ToDate(date2.Subtract(Convert.ToDateTime(date2))

Pls mark it as a solution if it solves your issue

Thanks

5 Likes

Hi,

In my environment, it returns 6 as the following. Did you change your locale in the project?

I guess “-172” is caused by 03/09/2023 is recognized as “dd/MM/yyyy”.
So can you try DateTime.ParseExact instead of Convert.ToDateTime as the following?

DateDiff(DateInterval.Day, DateTime.ParseExact(date_ETADate,"MM/dd/yyyy",System.Globalization.CultureInfo.InvariantCulture), convert.ToDateTime(date_ATADate))

Regards,

1 Like

It is reading both as dd/MM/yyyy. You need to inform it which date format you’re using if you’re going to mix and match like that.
image

If you want to waste a day trying to understand the minutiae of .NET CultureInfo then there’s this:

Otherwise you can use ParseExact to explicitly declare what kind of date format you’re giving it, eg:

DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);

1 Like

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