开发中关于日期的处理:NSDate中取出日期、取出时间等
来源:互联网 发布:网吧优化工具 编辑:程序博客网 时间:2024/06/05 09:04
有时候开发中我们只需要年月日、或者只需要时分秒,刚开始由于对NSDate不是很熟悉,走了很多弯路,特在此记录!!回头要好好把NSDate的文档好好看看
NSDate *date = _datePickerView.date; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; // Get Current Hour [formatter setTimeStyle:NSDateFormatterShortStyle]; NSLog(@"test------ %@",[formatter stringFromDate:date]);<pre name="code" class="objc">//这样设置得到的格式是这样的: test------ 下午2:19
[formatter setDateStyle:NSDateFormatterShortStyle]; NSLog(@"test+_+++++++ %@",[formatter stringFromDate:date]);
//test+_+++++++ 14-8-12 下午2:19
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(@"test+_+++++++ %@",[formatter stringFromDate:date]);
//test+_+++++++ 14-8-12
由上面可以知道,我们可以通过设置 setDateStyle 和setTimeStyle的格式来分别得到我们想要的结果:日期、时间等
如果我们想获取年、或者月、或者日、或者小时、或者分钟等单个值时该如何做呢?看代码如下:
NSDate *date = [NSDate date]; // Get Current Year NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy"]; NSString *currentyearString = [NSString stringWithFormat:@"%@", [formatter stringFromDate:date]]; // Get Current Month [formatter setDateFormat:@"MM"]; currentMonthString = [NSString stringWithFormat:@"%d",[[formatter stringFromDate:date]integerValue]]; // Get Current Date [formatter setDateFormat:@"dd"]; NSString *currentDateString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]]; // Get Current Hour [formatter setDateFormat:@"hh"]; NSString *currentHourString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]]; // Get Current Minutes [formatter setDateFormat:@"mm"]; NSString *currentMinutesString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]]; // Get Current AM PM [formatter setDateFormat:@"a"]; NSString *currentTimeAMPMString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
0 0
- 开发中关于日期的处理:NSDate中取出日期、取出时间等
- 关于jdk中API取出的日期和本地系统时间不一致的解决
- excel中取出的日期数字改成java日期
- iOS中时间/日期处理NSDate的常用接口操作
- 关于mysql取出数据库中连续日期(值)的例子,实现Oracle的row_number()
- NSDate 日期,时间处理
- Mybatis取出来的Date没有时间只有日期
- JS取出今天得日期时间
- 【OC加强】NSDate的用法——日期时间在实际开发中比较实用
- xcode 中nsdate 日期的一些处理方法
- .NET 中关于日期时间的格式化处理
- c#.NET 中关于日期时间的格式化处理
- c#.NET 中关于日期时间的格式化处理 (转)
- .NET 中关于日期时间的格式化处理
- jsp 中关于日期时间处理的几个问题
- C#.NET 中关于日期时间的格式化处理 (转)
- 在mysql数据库中关于日期时间字段的处理
- iOS开发-时间日期NSDate
- HttpClient session --------------cocos2d-x3.0正式版本(7.23)
- AOSP eclipse
- Ginger Breaker 获得root权限原理分析
- ExtJS4.2学习(七)EditorGrid可编辑表格
- redhat linux下面mysql的安装
- 开发中关于日期的处理:NSDate中取出日期、取出时间等
- 真正解决ASP.NET每一个页面首次访问超级慢的问题
- 机房收费系统(3)-梳理关系
- 关于IOS后台执行
- Caused by: java.lang.UnsatisfiedLinkError: 错误
- 编程之美--2.2 不要被阶乘吓倒
- 软技巧相关【5001】Team管理之工作梳理
- 056:hibernate树状结设计(至关重要)
- HDU 2087 剪花布条 KMP题解