c# - How to Convert string "07:35" (HH:MM) to TimeSpan -
c# - How to Convert string "07:35" (HH:MM) to TimeSpan -
i know if there way convert 24 hr time formatted string timespan.
right have "old fashion style":
string stringtime = "07:35"; string[] values = stringtime.split(':'); timespan ts = new timespan(values[0], values[1], 0);
while right work:
timespan time = timespan.parse("07:35");
and if using validation...
timespan time; if (!timespan.tryparse("07:35", out time)) { // handle validation error }
consider timespan
intended work elapsed time, rather time-of-day. take values larger 24 hours, , take negative values also.
if need validate input string valid time-of-day (>= 00:00 , < 24:00), should consider instead:
datetime dt; if (!datetime.tryparseexact("07:35", "hh:mm", cultureinfo.invariantculture, datetimestyles.none, out dt)) { // handle validation error } timespan time = dt.timeofday;
as added benefit, parse 12-hour formatted times when or pm included, long provide appropriate format string, such "h:mm tt"
.
c# timespan
Comments
Post a Comment