NSDateFormatter wrong string after formatting

2019-01-29 04:50发布

I receive a date through a string parameter, which is tempDateString, in a [day month year] format (for ex. 01 05 2005):

 NSLog(@"tempdatestring %@", tempDateString);
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"dd MM YYYY"];
 NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
 NSLog(@"daydate %@", dayDate);

My problem is, that the two logs don't match. The outputs are:

tempdatestring 04 10 2012
daydate 2011-12-24 22:00:00 +0000

What should I change at the date formatter's date format, to get the good date?

4条回答
The star\"
2楼-- · 2019-01-29 05:01
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:@"dd MM yyyy"];
form.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:-7200.0];
NSDate *dayDate = [form dateFromString:@"05 10 2012"];
NSLog(@"daydate %@", dayDate);
NSString *strDate = [form stringFromDate:dayDate];
NSLog(@"strDate %@",strDate);

Change date format to @"dd MM yyyy". After this, dateFromString may still parse the wrong date (in my case it was yesterday 21-00). To avoid this I've set TimeZone in my DateFormatter:

form.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:-7200.0];

-7200.0 is my timezone, you should change this to yours ("0" sets to Greenwich). After this log looks like:

daydate 2012-10-05 02:00:00 +0000
strDate 05 10 2012
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-29 05:07

2 Problems

  • your format is wrong it is @"dd MM yyyy" case sensitive
  • Use timezone to get the correct value[GMT value]

    NSString *tempDateString=@"04 10 2012" ;
    NSLog(@"tempdatestring %@", tempDateString);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"dd MM yyyy"];
    NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
    NSLog(@"daydate %@", dayDate);
    
查看更多
对你真心纯属浪费
4楼-- · 2019-01-29 05:11

When you use the %@ format specifier, the return value of the -description method invoked on the provided object is used.

NSDate's -description method outputs its value in that specific way.

Your real problem though is that your date format string is incorrect - it should be dd MM yyyy.

I stuck this in a sample Xcode project:

NSString *s = @"04 11 2012";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd MM yyyy"];
NSDate *d = [df dateFromString:s];
NSDateComponents *c = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:d];
NSLog(@"%@", c);

It gave me the following output:

2012-10-04 01:53:24.320 dftest[59564:303] <NSDateComponents: 0x100113e70>
    Calendar Year: 2012
    Month: 11
    Leap month: no
    Day: 4
查看更多
放荡不羁爱自由
5楼-- · 2019-01-29 05:21

Do this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MM yyyy"];
NSDate *dayDate = [dateFormatter dateFromString:tempDateString];
NSLog(@"daydate %@", dayDate);
NSString *strDate = [dateFormatter stringFromDate:dayDate];
NSLog(@"strDate :%@",strDate);
查看更多
登录 后发表回答