NSDate 格式化 及 互转
- /*
- NSDateFormatter的作用
- 1.NSString -> NSDate
- 2.NSDate -> NSString
- */
- void fmt_date_to_string();
- void fmt_string_to_date();
- void fmt_string_to_date2();
- void fmt_timestamp_to_date3();
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- // 2015-10-01 14:38:40
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
- NSDate *createdAtDate = [fmt dateFromString:@"2015-9-15 23:59:59"];
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSLog(@"%d", [calendar isDateInTomorrow:createdAtDate]);
- }
- return ;
- }
- void calendar_interval_between_date()
- {
- // 获得系统当前时间
- NSDate *nowDate = [NSDate date];
- // 服务器返回的时间字符串
- NSString *createdAtString = @"2014-02-20 10:49:54";
- // 创建一个日期格式化对象
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- // 设置日期格式
- fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
- // NSString -> NSDate
- NSDate *createdAtDate = [fmt dateFromString:createdAtString];
- // 计算createdAtDate和nowDate的时间间隔
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
- NSDateComponents *cmps = [calendar components:unit fromDate:createdAtDate toDate:nowDate options:];
- NSLog(@"%@", cmps);
- }
- /**
- * 获得某个NSDate对象的所有日期元素 : 年月日时分秒
- */
- void get_components_of_date()
- {
- // 获得系统当前时间
- NSDate *nowDate = [NSDate date];
- // 日历对象
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
- NSDateComponents *cmps = [calendar components:unit fromDate:nowDate];
- NSLog(@"%@", cmps);
- // NSInteger year = [calendar component:NSCalendarUnitYear fromDate:nowDate];
- // NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:nowDate];
- // NSLog(@"%zd %zd", year, minute);
- // NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- // // 设置日期格式
- // fmt.dateFormat = @"HH";
- // NSString *string = [fmt stringFromDate:nowDate];
- // NSLog(@"%@", string);
- }
- /**
- * 2个NSDate之间的时间差值
- */
- void date_interval()
- {
- // 获得系统当前时间
- NSDate *nowDate = [NSDate date];
- // 服务器返回的时间字符串
- NSString *createdAtString = @"2015-10-16 10:49:54";
- // 创建一个日期格式化对象
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- // 设置日期格式
- fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
- // NSString -> NSDate
- NSDate *createdAtDate = [fmt dateFromString:createdAtString];
- // 计算createdAtDate和nowDate之间相隔的秒数
- NSTimeInterval interval = [nowDate timeIntervalSinceDate:createdAtDate];
- NSLog(@"%f", interval);
- // 3600 / 60 = 60
- // 60 /60 = 1
- }
- /**
- * timestamp -> NSDate
- */
- void fmt_timestamp_to_date3()
- {
- // 时间戳 : 从1970年1月1日0点0分0秒开始经历的毫秒数
- NSInteger timestamp = ;
- NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];
- NSLog(@"%@", date);
- }
- /**
- * NSString -> NSDate
- */
- void fmt_string_to_date2()
- {
- // 服务器返回的时间字符串
- NSString *string = @"Tue May 31 17:46:55 +0800 2011";
- // 创建一个日期格式化对象
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- // 解析欧美格式的日期字符串, 得设置语言类型为en_US
- fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
- // 设置日期格式
- fmt.dateFormat = @"EEE MMM dd HH:mm:ss ZZZZ yyyy";
- NSDate *date = [fmt dateFromString:string];
- NSLog(@"%@", date);
- }
- /**
- * NSString -> NSDate
- */
- void fmt_string_to_date()
- {
- // 服务器返回的时间字符串
- NSString *string = @"2015-10-16 10:49:54";
- // 创建一个日期格式化对象
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- // 设置日期格式
- fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
- NSDate *date = [fmt dateFromString:string];
- NSLog(@"%@", date);
- }
- /**
- * NSDate -> NSString
- */
- void fmt_date_to_string()
- {
- // 获得系统当前时间
- NSDate *now = [NSDate date];
- // 创建一个日期格式化对象
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- // 设置日期格式 : 2015年10月10日 15时56分30秒
- // 年(year) : y
- // 月(month) : M
- // 日(day) : d
- // 时(hour) : H(24小时制)\h(12小时制)
- // 分(minute) : m
- // 秒(second) : s
- // 时区(zone) : Z
- fmt.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒";
- // NSDate -> NSString
- NSString *nowString = [fmt stringFromDate:now];
- NSLog(@"----- %@", nowString);
- }
NSDate 格式化 及 互转的更多相关文章
- NSDate 格式化 NSDate to NSString
NSLog(@"%@",[NSDate stringFromDate:[NSDate date] withFormat:@"yyyyMMdd__HH_mm_ss_zzz& ...
- NSDate 格式化含有毫秒
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; 版权声明:本文为博主原创文章,未经博主允许不得转载.
- iOS中NSDate常用转换操作整合
//当前时间格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss + (NSString *)getCurrentDataWithDateFormate:(NSString *)format ...
- oracle常规使用(一)
目录 特殊sql distinct 项目中遇到表中无主键,但是某个字段不能重复. 需要匹配id串里的内容 批量更新,但是批量成功返回的是-1 时间格式化 行列互转 应用场景 列转行 总结 oracle ...
- 时间戳和LocalDateTime和Date互转和格式化
一 前言 续上篇java8在日常开发中使用LocalDate和LocalTime[https://blog.csdn.net/youku1327/article/details/102771936]中 ...
- iOS NSDate获取当前时间并格式化
NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM- ...
- day04-交互、格式化输出及基本运算符
目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...
- NSString 常见数据类型转换:转NSInteger , NSDate(互转)
1. NSString转NSInteger, 转int (float, double类似 ) 1.1正常情况 , NSString所包含内容确能转化为int的类型 NSString *sNumber ...
- DAY04 与用户交 互格式化输出与运算符
与用户交互 输入: input # python2与python3的区别 # python3 res = input('please in put your username>>>& ...
随机推荐
- BackboneJS and UnderscoreJS
介绍 来自API(backbone能做什么?) 当我们开发含有大量Javascript的web应用程序时,首先你需要做的事情之一便是停止向DOM对象附加数据. 通过复杂多变的jQuery选择符和回调 ...
- Detectron:Pytorch-Caffe2-Detectron的一些跟进
pytorch官网:http://pytorch.org/上只有PyTroch的ubuntu和Mac版本,赤裸裸地歧视了一把Windows低端用户. 1. Caffe源码:Caffe源 ...
- (转)淘淘商城系列——Solr的安装
http://blog.csdn.net/yerenyuan_pku/article/details/72874134 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service ...
- MFC_2.10选项卡控件的封装
选项卡控件的封装 1.新建默认MFC项目 2.添加资源Dialog,属性style改child,边框改none,添加类取名CMyDialog1: 同理,CMyDialog2: 3.类向导,添加MFC类 ...
- 梦想CAD控件安卓界面控制
CAD控件界面上所有元素都可以控制显示或隐藏,下面将逐一介绍详细用法. 设置工具文件 MxFunction.setToolFile 设置工具文件.详细说明如下: 参数 说明 String sFile ...
- 梦想CAD控件 2018.10.15更新
下载地址: http://www.mxdraw.com/ndetail_10105.html 1. 完善com接口的ToCurves函数,转换CAD文字,多行文字到曲线 2. 修改DrawImage接 ...
- day02python
''' 列表 定义:在[]内,可以存放多个任意类型的值,并以逗号隔开. 一般用于存放学生的爱好,课堂的周期等等... ''' students=['钱垚','李小龙','张全蛋','赵铁柱'] pri ...
- Linux安装redis并且连接内网的redis
1.安装redis步骤 1.首先准备工作 [root@10-100-14-130 ~]# yum install gcc-c++ yum install wget 2.推荐进入到linux路径/ ...
- iOS的影片播放 MediaPlayer 和 AVPlayer
在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因為很方便使用,所以就一直使用下去.但是隨著客戶的要求越來越嚴苛,尤其是過場動畫或互動效果上的表 ...
- LeetCode_16 3SumCloest
3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...