I have a date range come like this,
string ActualReleaseDates ="7/8/2016, 7/9/2016, 7/11/2016,7/3/2016,7/10/2016,7/17/2016,7/24/2016,7/31/2016";
string NewsReleasedDate ="07/11/2016";
I want to check NewsReleaseDate is inside the ActualReleaseDates
But in the following code it return as a false.
if (ActualReleaseDates.Split(',').Contains(NewsReleasedDate.TrimStart(new Char[] { '0' })))
{
//some code here
}
The immediate problem is that after splitting your ActualReleaseDates
string, there isn't an entry of "7/11/2016"
... instead, there's an entry of " 7/11/2016"
... note the space.
But more fundamentally, just trimming the start of NewsReleasedDate
won't help if the value is something like "07/08/2016"... what you should be doing is handling these values as dates, rather than as strings:
ActualReleaseDates
by comma, then parse each value (after trimming whitespace) in an appropriate format (which I suspect is M/d/yyyy
) so that you get a List<DateTime>
.NewsReleasedDate
in the appropriate format, which I suspect is MM/dd/yyyy
, so you get a DateTime
.(I'd personally recommend using Noda Time and parsing to LocalDate
values, but I'm biased...)
Fundamentally, you're trying to see whether one date occurs in a list of dates... so make sure you get your data into its most appropriate representation as early as possible. Ideally, avoid using strings for this at all... we don't know where your data has come from, but if it started off in another representation and was converted into text, see if you can avoid that conversion.
See more on this question at Stackoverflow