.net - Split time to two variables in C# -
i wonder how can split time in 2 variables.
i time this
string mytime = console.readline();
and if type in 12:14 how can 12 in 1 variable , 14 in another?
parse timespan
, pull out parts way:
timespan ts = timespan.parse("12:14"); int hours = ts.hours; int minutes = ts.minutes;
one upside using timespan
validates you. when paired tryparse
method, can produce highly reliable code:
timespan ts; if (timespan.tryparse("12:99", out ts)) { // string valid time, use } else { // string not valid time, handle scenario }
Comments
Post a Comment