本文介绍了简单的日期格式化问题yyyymmdd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道将yyyymmdd这样的字符串转换成格式为mm-dd-yyyy的字符串的最佳方式。试过NSDateFormatter没有运气。任何帮助都不胜感激!



我的解决方案如下:

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

[dateFormatter setDateFormat:@MMM d,YYYY];
dateString = [dateFormatter stringFromDate:date];
[dateFormatter release];


解决方案

您首先需要解析:

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

然后你可以使用这个:

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


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:date];
[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];

这篇关于简单的日期格式化问题yyyymmdd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 16:48