NSDateFormatter wrong string after formatting

后端 未结 4 1702
独厮守ぢ
独厮守ぢ 2020-12-22 00:55

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

 NSLog(@\"tempdatestring %@\", tempD         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 00:58

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

提交回复
热议问题