-5

我从网络服务中得到一个字符串,pubDate:"Fri, 19 Jul 2013 07:13:44 GMT" 我想以 HH:mm:ss 格式打印时间,即我想打印 07: 13:44。我怎样才能做到这一点?

4

2 回答 2

1

NSString *pubDateString = [NSString stringWithFormat:@"Fri, 19 Jul 2013 07:13:44 GMT"];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];

NSDate *pubDate = [df dateFromString:pubDateString];

[df setDateFormat:@"HH:mm:ss"];
[df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSLog(@"%@",[df stringFromDate:pubDate]);

输出:07:13:44

苹果的日期格式指南:http: //developer.apple.com/library/ios/#documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

于 2013-07-19T08:34:28.230 回答
1

用这个 :

NSString *str = @"Fri, 19 Jul 2013 07:13:44 GMT";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [df dateFromString:str];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[df setTimeZone:sourceTimeZone];

NSDate *dateFromString = [df dateFromString:str];
[df setDateFormat:@"HH:mm:ss"];

// This is the time you need
NSString *timeIs = [df stringFromDate:date];
于 2013-07-19T08:21:36.180 回答