iOS NSString与NSDate互相转换及NSDate比较
2015-06-14 13:41
267 查看
由 NSDate 转换为 NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
结果:
2015-06-14 13:01:03
由 NSString 转换为 NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
NSLog(@"%@", date);
结果:
2015-06-14 13:01:03 +0800
日期之间比较可用以下方法
- (BOOL)isEqualToDate:(NSDate *)otherDate;
与otherDate比较,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
与anotherDate比较,返回较早的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
与anotherDate比较,返回较晚的那个日期
- (NSComparisonResult)compare:(NSDate *)other;
该方法用于排序时调用:
. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
结果:
2015-06-14 13:01:03
由 NSString 转换为 NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
NSLog(@"%@", date);
结果:
2015-06-14 13:01:03 +0800
日期之间比较可用以下方法
- (BOOL)isEqualToDate:(NSDate *)otherDate;
与otherDate比较,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
与anotherDate比较,返回较早的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
与anotherDate比较,返回较晚的那个日期
- (NSComparisonResult)compare:(NSDate *)other;
该方法用于排序时调用:
. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
相关文章推荐
- iOS开发学习笔记(一)——Hello,World!
- IOS笔记048-数据存储
- iOS7 StatusBar 使用小结
- iOS抽屉效果和侧边菜单
- iOS实现tableView下拉搜索功能
- IOS开发系列文章
- IOS开发入门书籍
- 【iOS官方文档翻译】iOS蓝牙的基本概念
- ios项目开发流程
- IOS的Crash情况在Crashlytics平台上统计解决方案的一点遗憾(截止到2015年6月14日)
- ios中关于delegate(委托)的使用心得
- 关于IOS下载缓存的文件
- 苹果开发 笔记(37)oc与JS调用
- 使用面向 iOS 的本机插件扩展 PhoneGap
- Xcode5 + phoneGap2.9搭建ios开发环境-配置-测试-归档上传/phoneG...
- ios加载文件中的数据(plist)
- IOS7之后控制TableViewSeparateView的宽度
- IOS不用AutoLayout也能实现自动布局的类(1)----MyLinearLayout横空出世
- IOS指示器
- iOS系统 查看序列号