2
int hour = [[array objectAtIndex:0] intValue]; //hour = 17
int min = [[array objectAtIndex:1] intValue]; //min = 51
NSLog(@"hour value is %d",hour);
NSLog(@"min value is %d",min);
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

int year = [components year];
int month = [components month];
int day = [components day];
NSDateFormatter  *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;

[components setYear:year];
[components setMonth:month];
[components setDay:day];
[components setHour:hour];
[components setMinute:min];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDate *itemDate = [cal dateFromComponents:components];
NSLog(@"the date is %@",itemDate);

输出:日期是 2012-11-03 01:00:00 +0000 。但我希望输出为 2012-11-03 17:51:00 ..我怎么能得到它?我做错了什么?

4

2 回答 2

0

看来您的问题很简单,没有意识到NSDate将使用您自己的当前时区创建对象,但将使用 UTCNSLog打印出描述。NSDate由于您似乎处于 PDT(本周末晚些时候美国时钟发生变化时将是 PST),您NSLog应该显示所需的日期/时间,但它似乎是 7 小时(本周末后 8 小时)关闭。

要在当地时区显示您的日期,请使用NSDateFormatter将 转换NSDateNSString

于 2012-11-03T05:32:12.623 回答
0
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"the date is %@", dateString);
于 2012-11-03T10:46:10.823 回答