How to change the date format in iphone sdk

后端 未结 2 832
灰色年华
灰色年华 2021-01-07 11:06

I\'m getting Response from Webservices Date like \"2012-08-17T00:00:00\". I want to Display Date only like 17-08-2012. How Change Date format and Remove that time...

相关标签:
2条回答
  • 2021-01-07 11:47
    //String froms web service(ws) 
    
    NSString *stringFromWS =[NSString stringWithString:@"2012-08-17T00:00:00"];
    
    
    //date formatter for the above string
    NSDateFormatter *dateFormatterWS = [[NSDateFormatter alloc] init];
    [dateFormatterWS setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    
    NSDate *date =[dateFormatterWS dateFromString:stringFromWS];
    
    
    //date formatter that you want 
    
    NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
    [dateFormatterNew setDateFormat:@"dd-MM-yyyy"];
    
    NSString *stringForNewDate = [dateFormatterNew stringFromDate:date];
    
    NSLog(@"Date %@",stringForNewDate);
    

    also refer the link for more explanation http://mobiledevelopertips.com/cocoa/date-formatters-examples-take-2.html

    0 讨论(0)
  • 2021-01-07 11:59
    NSDate *today = [NSDate date];
    NSLog(@"Date today: %@", today);
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init] ;
    dateFormat.dateFormat =  @"dd-MM-yyyy";
    NSString *dateString = [dateFormat stringFromDate:today];
    NSLog(@"Date: %@", dateString);
    

    Also see the below date and time format styles

    NSDateFormatterNoStyle
    NSDateFormatterShortStyle
    NSDateFormatterMediumStyle
    NSDateFormatterLongStyle
    NSDateFormatterFullStyle
    

    You can set it like [dateFormat setDateStyle:NSDateFormatterShortStyle];

    0 讨论(0)
提交回复
热议问题