I am having errors with variables. I am trying to get the dates between two date
ID: 3919915 • Letter: I
Question
I am having errors with variables. I am trying to get the dates between two datetimepicker values and then put them in a return alldates list. i would like the alldates to be in string format.
string earliestdate = dateTimePicker1.Value.ToString("yyyyMMdd");
string latestdate = dateTimePicker2.Value.ToString("yyyyMMdd");
DateTime earliestDate = DateTime.ParseExact(earliestdate, "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime latestDate = DateTime.ParseExact(latestdate, "yyyyMMdd", CultureInfo.InvariantCulture);
List<DateTime> dateList = DatesBetween(earliestDate, latestDate);
List<DateTime> DatesBetween( earliestDate, DateTime latestDate)
{
List<DateTime> allDates = new List<DateTime>();
for (DateTime date = earliestdate; date <= latestdate; date = date.AddDays(1))
allDates.Add(date);
return allDates;
}
c# Windows form app
Explanation / Answer
If you want to fetch only the Date Values in aprticular format from the DateTimePicker, then do the following:-
// Get the Date value from datetimepicker1 and convert it to the format "yyyMMdd"
// Get the Date value from datetimepicker2 and convert it to the format "yyyMMdd"
string latestdate = dateTimePicker2.Value.Date.ToString("yyyyMMdd");
// Extract the date which is converted to the format "yyyMMdd"
// Extract the date which is converted to the format "yyyMMdd"
DateTime latestDate = DateTime.ParseExact(latestdate, "yyyyMMdd", null);
return allDates;
}
Please let me know in case of any clarifications required. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.