我得到了这个空白的日期和时间:
-(void)getDay
{
NSDate *choice = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"yy"];
year = [[dateFormatter stringFromDate:choice] intValue];
[dateFormatter setDateFormat:@"MM"];
month = [[dateFormatter stringFromDate:choice] intValue];
[dateFormatter setDateFormat:@"dd"];
day = [[dateFormatter stringFromDate:choice] intValue];
[dateFormatter setDateFormat:@"HH"];
hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];
// NSLog(@"hour: %d",hour);
[dateFormatter setDateFormat:@"mm"];
minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];
[dateFormatter setDateFormat:@"ss"];
second = [[dateFormatter stringFromDate:[NSDate date]] intValue];
}
如果我看到日志小时......它给了我当地时间的正确时间,但是当我将该值放入另一个 NSDate 时,我需要创建小时更改并成为格林威治时间,这是我使用的代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
// NSLog(@"hour: %d",hour);
//this log still gives me the right hour
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
self.myDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d %d:%d:%d",day,month,year,hour,minute,second]];
NSLog(@"my date: %@",myDate);
最后的日志如下所示:
2013-08-31 05:14:12.905 myApp[25987:904] 我的日期:2013-08-31 15:14:00 +0000
我的时间应该是 5:14,但格林威治时间是 15:14……为什么会发生这种情况,我怎样才能得到当地时间而不是格林威治时间?谢谢你的帮助。