I have the following EffectiveDay which I want to change datetime to the midnight.
public DateTime EffectiveDate { get; set; }

In the screenshot, it shows 02/28/2018 5:00:00 AM.
I want to change it to 02/28/2018 12:00:00 AM (midnight)
I tried the following approach, it did not work
ClassRoom.WorkOrders.Select(w => w.EffectiveDate).ToList().ForEach(s => s = s.Date);
I tried the following approach as well, it did not work.
ClassRoom.WorkOrders.Select(w => w.EffectiveDate).ToList().ForEach(s => s = GetDateZeroTime(s.Date));
public static DateTime GetDateZeroTime(DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
}
Doing ForEach with assignments after ToList are not going to help, because you are setting s, a parameter that is thrown away after each iteration.
Use a normal foreach loop to do the assignment:
foreach (var w in ClassRoom.WorkOrders) {
w.EffectiveDate = w.EffectiveDate.Date;
// w.EffectiveDate = GetDateZeroTime(w.EffectiveDate); would also work
}
This should effectively remove the time from all WorkOrders by assigning the EffectiveDate property of each one to a new DateTime object that has no knowledge of any kind of time:
foreach (var workOrder in ClassRoom.WorkOrders)
workOrder.EffectiveDate = new DateTime(workOrder.EffectiveDate.Year, workOrder.EffectiveDate.Month, workOrder.EffectiveDate.Day, 0, 0, 0);
Setting workOrder.EffectiveDate = workOrder.EffectiveDate.Date in the loop should also work just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With