Changing date format on iOS

前端 未结 2 1026
心在旅途
心在旅途 2020-12-11 12:16

I have some dates in this format :

2012-06-26 12:00:00 +0000

and what i want to do is convert them in this format :

Jun 26,         


        
相关标签:
2条回答
  • 2020-12-11 13:03

    First, you should convert your string (with a NSDateFormatter ) in a NSDate and than convert it back to a NSString (still with the same NSDateFormatter) with the target format.

    Here is an example of a NSDateFormatter :

    NSString *dateString = @"2013-04-01T22:00:00Z";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];
    NSDate *theDate = [dateFormatter dateFromString:dateString];
    
    0 讨论(0)
  • 2020-12-11 13:08

    To convert from 1 string to the other use the following

    NSString *inputString = @"2012-06-26 12:00:00 +0000";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];//input
    
    NSDate *date = [formatter dateFromString:inputString];
    
    [formatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];
    NSString *outputString = [formatter stringFromDate:date];
    NSLog(@"output : %@",outputString);
    
    0 讨论(0)
提交回复
热议问题