为什么我会收到“2010-01-01 00:00:00 +900”来自“2010-12-31 15:00:00“000”?
我有 NSDate,如果我使用 NSLog(@"%@", date.description); 则会显示如下:
2010-12-31 15:00:00 +0000
它将显示为好像我使用了 NSLog(@"%@", [date descriptionWithLocale:[[NSLocale currentLocale] localeIdentifier]]);
2011 年 1 月 1 日星期六上午 12:00:00 日本标准时间
但是如果我使用 NSLog(@"%@", [date formattedDateString]); 则会显示如下
2010-01-01 00:00:00 +0900
我哪里出错了?
- (NSString *)formattedDateString {
return [self formattedStringUsingFormat:@"YYYY-MM-dd HH:mm:ss ZZZ"];
}
- (NSString *)formattedStringUsingFormat:(NSString *)dateFormat {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:dateFormat];
NSString *ret = [formatter stringFromDate:self];
[formatter release];
return ret;
}
I have NSDate, it will be shown as below if I used NSLog(@"%@", date.description);
2010-12-31 15:00:00 +0000
it will be shown as if I used NSLog(@"%@", [date descriptionWithLocale:[[NSLocale currentLocale] localeIdentifier]]);
Saturday, January 1, 2011 12:00:00 AM
Japan Standard Time
But it will be show as below if I used NSLog(@"%@", [date formattedDateString]);
2010-01-01 00:00:00 +0900
Where do I make mistake?
- (NSString *)formattedDateString {
return [self formattedStringUsingFormat:@"YYYY-MM-dd HH:mm:ss ZZZ"];
}
- (NSString *)formattedStringUsingFormat:(NSString *)dateFormat {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:dateFormat];
NSString *ret = [formatter stringFromDate:self];
[formatter release];
return ret;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我得到了我需要的东西,如下:
I got what I need as below:
你有 NSDate 的子类,对吗?我唯一的猜测是您在覆盖代码的其他地方犯了一些错误,也许是在设置日期的时间部分(可以这么说)时,导致描述字符串或其源数据未正确更新。
编辑:哦,你成功了,太棒了。晚了几秒钟。 :) 干杯!
You have a subclass of NSDate, right? My only guess is that you made some mistake somewhere else in the override code, maybe when setting the time components of the date (so to speak) so that the description string or its source data was not updated correctly.
Edit: Oh you got it working, great. Couple of seconds too late. :) Cheers!
2010-01-01 00:00:00 +900
和2010-12-31 15:00:00 +000
都指向同一时间。编辑:
2011-01-01 00:00:00 +900
和2010-12-31 15:00:00 +000
都指向同一时间。您碰巧住在 GMT+9(比如东京)吗?
Both
2010-01-01 00:00:00 +900
and2010-12-31 15:00:00 +000
point to the same time.Edit:
Both
2011-01-01 00:00:00 +900
and2010-12-31 15:00:00 +000
point to the same time.Do you happen to live at GMT+9 (let's say Tokyo)?