我有以下内容:
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate* date = [df dateFromString:[sDate stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"]];
[df release];
我想输出字符串是“9/20/10”
我怎样才能做到这一点?
你可以这样做:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSString *result = [formatter stringFromDate:[NSDate date]];
这将在用户设置中显示(但以类似的方式),所以对我来说结果将是7/28/10
你也可以这样做:
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"MM/dd/yyyy"];
NSString *result = [df stringFromDate:date];
[df release];
你也可以试试
NSString *dateString = [NSString stringWithFormat:[NSDate date]];