您的位置:首页 > 其它

NSDate和NSDateFormatter

2016-01-01 15:40 330 查看
NSDate的一些基本用法

/**
*   日期与时间
*/
-(void)studyOne{

/*
OC 为处理日期和时间提供了NSDate和NSCalendar对象,还提供了日期格式器来处理日期与字符串之间的转换.
NSDate对象代表日期与时间, OC 既提供了类方法来创建NSDate对象,也通过了实例化方法.
*/

//获取当前日期
NSDate *date1=[NSDate date];
NSLog(@"date1 %@",date1);

//获取从当前时间开始,一天之后的日期
NSDate *date2=[[NSDate alloc]initWithTimeIntervalSinceNow:3600*24];
NSLog(@"date2 %@",date2);

//获取从当天时间开始,3天前的日期
NSDate *date3=[[NSDate alloc]initWithTimeIntervalSinceNow:-3*3600*24];
NSLog(@"date3 %@",date3);

//获取从1970年1月1号开始,20年后的日期
NSDate *date4=[NSDate dateWithTimeIntervalSince1970:3600*24*366*20];
NSLog(@"date4 %@",date4);

//获取系统当前的 Locale
NSLocale *cn=[NSLocale currentLocale];
//获取NSDate在当前Locale下对应的字符串
NSLog(@"%@",[date1 descriptionWithLocale:cn]);// Friday, January 1, 2016 at 2:35:47 PM China Standard Time

//获取两个日期之间较早的日期
NSDate *earlier=[date1 earlierDate:date2];
//获取两个日期之间较晚的日期
NSDate *later=[date1 laterDate:date2];
NSLog(@"%@ %@",earlier,later);

/*

比较两个日期,compare:方法返回NSComparisonResult,该枚举包括3各值:
typedef NS_ENUM(NSInteger, NSComparisonResult) {
NSOrderedAscending = -1L,
NSOrderedSame,
NSOrderedDescending
};分别是之前之前,相等,之后.

*/
switch ([date1 compare:date3]) {

case NSOrderedAscending:{
NSLog(@"date1位于date3之前");
}
break;
case NSOrderedSame:{
NSLog(@"date1与date3日期相等");
}
break;

case NSOrderedDescending:{
NSLog(@"date1位于date3之后");
}
break;

}

//获取两个时间之间的时间差
NSLog(@"date1与date3之间的时间差为%g 秒", [date1 timeIntervalSinceDate:date3]);

//获取指定时间与现在的时间差
NSLog(@"date2与现在时间差%g秒",[date2 timeIntervalSinceNow]);

}


NSDateFormatter的一些基本用法

/**
* NSDateFormatter
*/
-(void)studyFive{

/*
NSDateFormatter代表一个日期格式器,它的功能是完成NSDate与 NSString之间的转换.
使用NSDateFormatter完成NSDate与 NSString之间的转换 的步骤如下:

1)创建一个NSDateFormatter对象.
2)调用NSDateFormatter的setDateStyle:,setTimeStyle:方法设置格式化日期,时间的风格,其中日期,风格支持如下几个枚举值:
typedef NS_ENUM(NSUInteger, NSDateFormatterStyle) {    // date and time format styles
NSDateFormatterNoStyle = kCFDateFormatterNoStyle,         不显示日期,时间的风格
NSDateFormatterShortStyle = kCFDateFormatterShortStyle,   显示短的日期,时间风格
NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle, 显示中等日期,时间风格
NSDateFormatterLongStyle = kCFDateFormatterLongStyle,     显示长的日期,时间风格
NSDateFormatterFullStyle = kCFDateFormatterFullStyle      显示完整的日期,时间风格
};
如果打算使用自己的格式模版,调用NSDateFormatter的 setDateFormate:方法设置日期,时间的模版即可.
3)如果需要将NSDate转换为 NSString,调用NSDateFormatter的stringFromDate:方法执行格式化即可:相反,如果需要将NSString转换为NSDate,调用NSDateFormatter的dateFromString:方法执行格式化即可
*/

//获取从1970年1月1号开始,20年后的日期
NSDate *date=[NSDate dateWithTimeIntervalSince1970:3600*24*366*20];

NSLocale *locales[]={
[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"],
[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]
};

NSDateFormatter *formatter[8];

for (int i=0; i<2; i++) {

formatter[i*4]=[[NSDateFormatter alloc]init];
//设置NSDateFormatter的日期和风格
[formatter[i*4] setDateStyle:NSDateFormatterShortStyle];
[formatter[i*4] setTimeStyle:NSDateFormatterShortStyle];
//设置NSDateFormatter的locale
[formatter[i*4] setLocale:locales[i]];

formatter[i*4+1]=[[NSDateFormatter alloc]init];
//设置NSDateFormatter的日期和风格
[formatter[i*4+1] setDateStyle:NSDateFormatterMediumStyle];
[formatter[i*4+1] setTimeStyle:NSDateFormatterMediumStyle];
//设置NSDateFormatter的locale
[formatter[i*4+1] setLocale:locales[i]];

formatter[i*4+2]=[[NSDateFormatter alloc]init];
//设置NSDateFormatter的日期和风格
[formatter[i*4+2] setDateStyle:NSDateFormatterLongStyle];
[formatter[i*4+2] setTimeStyle:NSDateFormatterLongStyle];
//设置NSDateFormatter的locale
[formatter[i*4+2] setLocale:locales[i]];

formatter[i*4+3]=[[NSDateFormatter alloc]init];
//设置NSDateFormatter的日期和风格
[formatter[i*4+3] setDateStyle:NSDateFormatterFullStyle];
[formatter[i*4+3] setTimeStyle:NSDateFormatterFullStyle];
//设置NSDateFormatter的locale
[formatter[i*4+3] setLocale:locales[i]];

}

for (int i=0; i<2; i++) {
switch (i) {
case 0:{
NSLog(@"-------中国日期格式-------");
}
break;
case 1:{
NSLog(@"-------美国日期格式-------");
}
break;

default:
break;
}

NSLog(@"short格式的日期%@",[formatter[i*4] stringFromDate:date]);
NSLog(@"medium格式的日期%@",[formatter[i*4+1] stringFromDate:date]);
NSLog(@"long格式的日期%@",[formatter[i*4+2] stringFromDate:date]);
NSLog(@"full格式的日期%@",[formatter[i*4+3] stringFromDate:date]);
}

//自定义模版
NSDateFormatter *date2=[[NSDateFormatter alloc]init];
[date2 setDateFormat:@"公元yyyy年MM月DD日HH时mm分"];
NSLog(@"%@",[date2 stringFromDate:date]);

NSString *dateStr=@"2016-01-01";
NSDateFormatter *date3=[[NSDateFormatter alloc]init];
//根据日期字符串模版设置格式模版
[date3 setDateFormat:@"yyyy-MM-dd"];

//将字符串转换为NSDate对象
NSDate *date4=[date3 dateFromString:dateStr];
NSLog(@"%@",date4);

/*
-------中国日期格式-------

short格式的日期:90/1/16 上午8:00
medium格式的日期:1990年1月16日 上午8:00:00
long格式的日期:1990年1月16日 GMT+8 上午8:00:00
full格式的日期:1990年1月16日 星期二 中国标准时间 上午8:00:00

-------美国日期格式-------

short格式的日期:1/16/90, 8:00 AM
medium格式的日期:Jan 16, 1990, 8:00:00 AM
long格式的日期:January 16, 1990 at 8:00:00 AM GMT+8
full格式的日期:Tuesday, January 16, 1990 at 8:00:00 AM China Standard Time

公元1990年01月16日08时00分
2015-12-31 16:00:00 +0000

*/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: