سؤال

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;
}
هل كانت مفيدة؟

المحلول

I got what I need as below:

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

نصائح أخرى

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!

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)?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top