How to convert Time difference types of times present in the excel sheet…
so many colums present in the sheet. i have to change this columns only
Input:
dt.AsEnumerable.ToList.ForEach(Sub(r)
r("Time")=DateTime.ParseExact(r("Date").ToString+" "+r("Time").ToString,formats,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("HH:mm")
End Sub
)
Because there is AM/PM in some cells. As we need to parse AM/PM using DataTime, the above expression uses Date column. And it’s also simple if blank cell exists.
dt.AsEnumerable.ToList.ForEach(Sub(r)
r("Time")=DateTime.ParseExact(r("Time").ToString,{"%H","H:m","H.m","h.m tt"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("HH:mm")
End Sub
)
This can handle even if AM/PM exists. However throw exception if blank cell exists.
C7 of the above xlsx is blank. If we need to handle blank cell, the following will work.
dt.AsEnumerable.ToList.ForEach(Sub(r)
If DateTime.TryParseExact(r("Time").ToString,{"%H","H:m","H.m","h.m tt"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None,New DateTime) Then
r("Time")=DateTime.ParseExact(r("Time").ToString,{"%H","H:m","H.m","h.m tt"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("HH:mm")
Else
r("Time")="00:00"
End If
End Sub
)