首先,传进来的时间String是按24小时格式,然后将这个时间转换成NSDate:
NSString* formatstring = nil;
if([NSLocale timeIs24HourFormat])
formatstring = @"yyyy-MM-dd hh:mm:ss";
else
formatstring = @"yyyy-MM-dd HH:mm:ss";
NSDate* date = [NSString stringToDate:datestring andDateFormat:formatstring setUTC:NO];
stringToDate: andDateFormat:setUTC函数实现:
+(NSDate*)stringToDate:(NSString*)dateStr andDateFormat:(NSString*)dateFormatString setUTC:(BOOL)b
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
if(b)
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:dateFormatString];//@"yyyy-MM-dd HH:mm:ss"];
NSDate* dateFromString = [dateFormatter dateFromString:dateStr];
return dateFromString;
}
最后,将这个dateFromString设置到UIDatePicker的date属性。