I have two dates.
ReleaseDates = "11/2/2016";
LiveDate = "11/02/2016";
In the above two dates are same. But in my below coding it returns FALSE.
if (ReleaseDates.Contains(LiveDate.TrimStart(new Char[] { '0' })))
{
}
I have two dates.
ReleaseDates = "11/2/2016";
LiveDate = "11/02/2016";
In the above two dates are same. But in my below coding it returns FALSE.
if (ReleaseDates.Contains(LiveDate.TrimStart(new Char[] { '0' })))
{
}
Your code does not work because TrimStart
removes characters at the start of the string. It looks like LiveDate
has no zeros at the beginning; the character '0'
that you want to trim is at index 3, preceded by other characters. That is why it is not getting trimmed.
Comparing strings representing dates is an error-prone strategy. You would be better off parsing both strings, and comparing the results as DateTime
objects.
In general, you should keep dates as objects designed for date representation - for example, DateTime
or DateTimeOffset
. This would let you customize date representation for display purposes, and avoid errors when date format changes from mm/dd to dd/mm.
This is because you are not comparing dates, you are comparing two strings that are not the same. Your best shot here is parsing first and then comparing.
DateTime releaseDate = DateTime.Parse(ReleaseDates);
DateTime liveDate = DateTime.Parse(LiveDate);
if (releaseDate == liveDate) // This should be true.
{
// Do stuff.
}