iPhone NSDateFormatter Timezone Conversion

前端 未结 6 1304
终归单人心
终归单人心 2020-12-03 01:15

I am trying to create a formatter that will convert the date format shown to an NSDate object:

NSString *dateStr = @\"2010-06-21T19:00:00-05:00\";
NSDateForm         


        
6条回答
  •  借酒劲吻你
    2020-12-03 01:22

    This is the default time format I got from a Sinatra ActiveRecord backend. Here is my solution.

    -(NSDate *) dateFromString:(NSString *)string{
        NSMutableString * correctedDateString = [[NSMutableString alloc] initWithString:string];
        [correctedDateString deleteCharactersInRange: NSMakeRange(22, 1)];
    
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
    
        return [formatter dateFromString:correctedDateString];
    }
    

提交回复
热议问题