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
Yoichi
(Yoichi)
March 3, 2023, 12:22am
3
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.
If you want to waste a day trying to understand the minutiae of .NET CultureInfo then there’s this:
Provides information about a specific culture (called a locale for unmanaged code development). The information includes the names for the culture, the writing system, the calendar used, the sort order of strings, and formatting for dates and...
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
system
(system)
Closed
March 6, 2023, 8:37am
5
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.