开发者

Simple date formatting question yyyymmdd

Wondering about the best way to turn a string like "yyyymmdd" into a string with the format "mm-dd-yyyy". Tried NSDateFormatter with no luck. Any help is appreciated!

My solution is as follows:

NSString *dateString = @"22082011;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormatter dateFromString:dateString];

[dateFormatter setDateFormat:@"MMM d, YYYY"];
dateString = [dateFormatter stringFromDate:dat开发者_运维百科e];
[dateFormatter release];


You will first need to parse it:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];  

and then you can use this:

[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString *myFormattedDate = [dateFormat stringFromDate:date];


NSString *dateStr = @"20081122";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];  

// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];  
[dateFormat release];

From a helpful article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜