objective c - Parse Time PT##H##M to NSDate -
i breaking string using substrings there should way parse string of specific format , out desired values.
please me if there other easy , elegant way this.
[update] how i'm doing using substrings:
nsrange ptrange = [timestring rangeofstring:@"t"]; nsrange hrange = [timestring rangeofstring:@"h"]; nsrange mrange = [timestring rangeofstring:@"m"]; nsstring *hours = nil; if (hrange.location != nsnotfound) { hours = [timestring substringwithrange:nsmakerange(ptrange.location+ptrange.length, hrange.location-(ptrange.location+ptrange.length))]; } else { hrange = ptrange; } nsstring *minutes = nil; if (mrange.location != nsnotfound) { minutes = [timestring substringwithrange:nsmakerange(hrange.location+hrange.length, mrange.location-(hrange.location+hrange.length))]; } nsdateformatter *df = [[nsdateformatter alloc] init]; [df setdateformat:@"hh:mm"]; nsdate*date = [df datefromstring:[nsstring stringwithformat:@"%d:%d",hours.integervalue,minutes.integervalue]];
as of implementation answer question
nsrange ptrange = [timestring rangeofstring:@"t"]; nsrange hrange = [timestring rangeofstring:@"h"]; nsrange mrange = [timestring rangeofstring:@"m"]; nsstring *hours = nil; if (hrange.location != nsnotfound) { hours = [timestring substringwithrange:nsmakerange(ptrange.location+ptrange.length, hrange.location-(ptrange.location+ptrange.length))]; } else { hrange = ptrange; } nsstring *minutes = nil; if (mrange.location != nsnotfound) { minutes = [timestring substringwithrange:nsmakerange(hrange.location+hrange.length, mrange.location-(hrange.location+hrange.length))]; } nsdateformatter *df = [[nsdateformatter alloc] init]; [df setdateformat:@"hh:mm"]; nsdate*date = [df datefromstring:[nsstring stringwithformat:@"%d:%d",hours.integervalue,minutes.integervalue]];
Comments
Post a Comment