converting date to correct format

后端 未结 2 1754
温柔的废话
温柔的废话 2021-01-23 18:57

I\'ve a webservice which gives back my date in the following way.

Wed Oct 31 11:59:44 +0000 2012

But I want it to give it back in this way

2条回答
  •  走了就别回头了
    2021-01-23 19:47

    Step 1: create an NSDateFormatter to convert your string from server to an NSDate object by setting the format to the format of the "server string"

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"E MMM d hh:mm:ss Z y"];
    NSDate *date = [f dateFromString:@"Wed Oct 31 11:59:44 +0000 2012"];
    

    Step 2: create another NSDateFormatter with the desired output string and convert your new NSDate object to a string object using the new NSDateFormatter

    NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
    [f2 setDateFormat:@"dd-MM-y hh:mm"];
    [f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSString *s = [f2 stringFromDate:date];
    
    desiredformat = s;
    

    P.S. I'm not sure of f format, check this link http://www.developers-life.com/nsdateformatter-and-uifont.html

提交回复
热议问题