how to convert string into date format in iOS?

后端 未结 2 655
天涯浪人
天涯浪人 2021-01-26 06:09

I have string from web service like 12/31/2013 09:12:15 A.M. Now I Want convert it like 12 Dec 2013 09:12:15 A.M. with the use of NSDAteFormatter

相关标签:
2条回答
  • 2021-01-26 06:39

    I'm giving you the answer, but downvoting your question for being too elementary and whose solution could easily found with a simple search.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    NSDate *date = [formatter dateFromString:myDateString];
    [formatter setDateFormat:@"dd MMMM yyyy aa:mm:ss a"];
    NSString *newDate = [formatter stringFromDate:date];
    
    0 讨论(0)
  • 2021-01-26 06:40
    NSString *dateStr = @"12/31/2013 09:12:15 AM";
    
    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    NSDate *date = [dateFormat dateFromString:dateStr];  
    
    // Convert date object to desired output format
    [dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
    dateStr = [dateFormat stringFromDate:date];  
    [dateFormat release];
    
    0 讨论(0)
提交回复
热议问题