0

我的日期选择器返回错误的分钟和秒值,但小时工作正常。这是我的获取方法。

-(NSDate *)datePickerTime
{
NSCalendar *gregorianCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
[gregorianCalendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"HH:mm:ss"];
[format setTimeZone:[NSTimeZone localTimeZone]];
NSString *dateString =[format stringFromDate:[self.timePicker date]];
NSDate *adjustedDate = [format dateFromString:dateString];
NSDateComponents *comp = [[NSDateComponents alloc]init];

comp = [gregorianCalendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:adjustedDate];

reminderTime = [gregorianCalendar dateFromComponents:comp];

return reminderTime;
}
4

2 回答 2

1

请检查具有所有类型格式支持的日期选择器的最佳Apple 示例代码!

您的问题可能出在setDateFormat ISO-8601

试试这个代码

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"hh:mm:ss a"];

NSLog(@"Current Date: %@", [formatter stringFromDate:[NSDate date]]);

[formatter release];
于 2013-04-03T11:34:36.613 回答
0

在我的应用程序中,我通过以下方式获得正确的时间

NSDate *endDate = [NSDate date];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"HH:mm:ss"];
NSString* curdatestr = [dateFormatter2 stringFromDate:endDate];
NSLog(@"str%@",curdatestr);

如果您还想要使用当前时间的日期

[dateFormatter2 setDateFormat:@"yyyy-MM-dd 'at' HH:mm:ss"]; 
于 2013-04-03T12:00:03.097 回答