为什么我会收到“2010-01-01 00:00:00 +900”来自“2010-12-31 15:00:00“000”?

发布于 2024-10-10 04:54:19 字数 898 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思慕 2024-10-17 04:54:19

我得到了我需要的东西,如下:

-(NSString *)formattedDateString {
return [self formattedStringUsingFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
}

I got what I need as below:

-(NSString *)formattedDateString {
  return [self formattedStringUsingFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
}

一场春暖 2024-10-17 04:54:19

你有 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!

爱你是孤单的心事 2024-10-17 04:54:19

2010-01-01 00:00:00 +9002010-12-31 15:00:00 +000 都指向同一时间。

编辑
2011-01-01 00:00:00 +9002010-12-31 15:00:00 +000 都指向同一时间。

您碰巧住在 GMT+9(比如东京)吗?

Both 2010-01-01 00:00:00 +900 and 2010-12-31 15:00:00 +000 point to the same time.

Edit:
Both 2011-01-01 00:00:00 +900 and 2010-12-31 15:00:00 +000 point to the same time.

Do you happen to live at GMT+9 (let's say Tokyo)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文