parsing a particular time format
I am getting a time/d开发者_StackOverflow中文版uration from a .NET web service in this format: PT12H30M
How can this be handled?
When you define the format string for parsing a date you use the UTS #35 Date Format Patterns to define what the incoming data looks like. PT looks like a legitimate timezone abbreviation. You will want to fiddle with this depending on whether the minutes and hours are zero-padded or not.
NSString *dateString = @"PT12H30M";
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ZZhh'H'mm'M'"];
NSDate date = [dateFormatter stringFromDate:dateString];
Take a look at NSDateFormatter, which can parse dates with a user-specified format using particular syntax. Parse result is a standard NSDate.
Create an instance of NSDateFormatter configured with a format string the describes the time format, and then use the -dateFromString:
method to translate the time you get from the web service into a NSDate object.
精彩评论