I am on a simulation application and need to attach different time with each record. I am storing the date and time in separate strings:
string pdate = DateTime.Now.ToShortDateString();
string ptime = DateTime.Now.ToShortTimeString();
Now I am trying to add 5 minutes to the ptime variable (this I am doing in a foreach loop).
foreach (loaddata dl2 in lstdata)
{
//some code
ptime = DateTime.Now.AddMinutes(5);
//Results in ptime with date and time(I only need the time here)
}
UPDATE: I need to add 5 minutes to the previous time in each iteration
Suggestions please.
Do this way
Problem with your code
Store DateTime in DateTime only so you can “increment” them there itself.
Converting from DateTime to string is easy and almost error free. But conversion from string to Dateime sometimes results in exception.
Best is to do it this way
Conversion to string should be done when you know that DateTime Format is not required like showing in UI.