1、如何如何将一个字符串如“ 20110826134106”装化为任意的日期时间格式,下面列举两种类型:
NSString* string =@"20110826134106";
NSDateFormatter*inputFormatter = [[[NSDateFormatter alloc] init]autorelease];
[inputFormattersetLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];
[inputFormattersetDateFormat:@"yyyyMMddHHmmss"];
NSDate*inputDate = [inputFormatter dateFromString:string];
NSLog(@"date= %@", inputDate);
NSDateFormatter*outputFormatter = [[[NSDateFormatter alloc] init]autorelease];
[outputFormattersetLocale:[NSLocale currentLocale]];
[outputFormattersetDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
NSString*str = [outputFormatter stringFromDate:inputDate];
NSLog(@"testDate:%@",str);
两次打印的结果为:
date= 2011-08-26 05:41:06 +0000
testDate:2011年08月26日13时41分06秒
2、iOS-NSDateFormatter 格式说明:
G: 公元时代,例如AD公元
yy:年的后2位
yyyy:完整年
MM:月,显示为1-12
MMM:月,显示为英文月份简写,如 Jan
MMMM:月,显示为英文月份全称,如 Janualy
dd:日,2位数表示,如02
d:日,1-2位显示,如 2
EEE:简写星期几,如Sun
EEEE:全写星期几,如Sunday
aa:上下午,AM/PM
H:时,24小时制,0-23
K:时,12小时制,0-11
m:分,1-2位
mm:分,2位
s:秒,1-2位
ss:秒,2位
S:毫秒
常用日期结构:
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy
3:新浪微博接口中,取得日期字符串 的转换
NSString*dateStr = @"Wed May 222:27:08+0800 2012";
NSDateFormatter* formater =[[NSDateFormatter alloc] init];
[formater setDateFormat:@"EEE MMM d HH:mm:sszzzz yyyy"];
NSDate* date = [formaterdateFromString:dateStr];
NSLog(@"%@",date);
//NSDate* now = [NSDatenow];
double inter = fabs([datetimeIntervalSinceNow]);
if( inter <60)
NSLog(@"1 mins ago!");
else if(inter< 60*60)
NSLog(@"1 hours ago!");
else if(inter< 60*60*24)
NSLog(@"1 days ago!");
NSLog(@"interval
is %f min", inter/60);
上面代码在真机上运行后,发现取得date为NULL,模拟器正常显示,上网搜索后发现需要设置local, 果然设置后,真机正常
NSLocale* local =[[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] autorelease];
[formatter setLocale: local];
4、自定义显示的 星期 格式
使用NSDateFormatter转换日期时,得到的英文字母的星期几只能是这样,如Sun, Mon, etc.
如果想得到大写字母的星期几,可以这样:
NSArray*weekdayAry = [NSArray arrayWithObjects:@"SUN",
@"MON", @"TUE",@"WED", @"THU", @"FRI", @"SAT", nil];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSLocalizedString(@"YYYY.MM.dd.eee",nil)];
//此处更改显示的大写字母的星期几
[dateFormattersetShortWeekdaySymbols:weekdayAry];
[dateFormatter setLocale:[[NSLocale
alloc]initWithLocaleIdentifier:@"en_US"] ]];
NString *str= [dateFormatter stringFromDate:[NSDate
date]];
5、计算距离某一天还有多少时间
NSDate* toDate = [ [ NSDate alloc]initWithString:@"2012-9-29 0:0:00 +0600" ];
NSDate* startDate = [ [ NSDatealloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ]initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags =
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [chineseClendar components:unitFlagsfromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [cps hour];
NSInteger diffMin = [cpsminute];
NSInteger diffSec = [cps second];
NSInteger diffDay = [cps day];
NSInteger diffMon = [cps month];
NSInteger diffYear = [cps year];
NSLog( @" From Now to %@, diff: Years:%d Months: %d, Days; %d, Hours: %d, Mins:%d,sec:%d",
[toDate description], diffYear, diffMon, diffDay, diffHour,diffMin,diffSec );
IOS --- 日期时间格式 转换,,5-wow.com