1. /*
  2. NSDateFormatter的作用
  3. 1.NSString -> NSDate
  4. 2.NSDate -> NSString
  5. */
  6. void fmt_date_to_string();
  7. void fmt_string_to_date();
  8. void fmt_string_to_date2();
  9. void fmt_timestamp_to_date3();
  10.  
  11. int main(int argc, const char * argv[]) {
  12. @autoreleasepool {
  13. // 2015-10-01 14:38:40
  14. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  15. fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  16. NSDate *createdAtDate = [fmt dateFromString:@"2015-9-15 23:59:59"];
  17.  
  18. NSCalendar *calendar = [NSCalendar currentCalendar];
  19. NSLog(@"%d", [calendar isDateInTomorrow:createdAtDate]);
  20. }
  21. return ;
  22. }
  23.  
  24. void calendar_interval_between_date()
  25. {
  26. // 获得系统当前时间
  27. NSDate *nowDate = [NSDate date];
  28.  
  29. // 服务器返回的时间字符串
  30. NSString *createdAtString = @"2014-02-20 10:49:54";
  31. // 创建一个日期格式化对象
  32. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  33. // 设置日期格式
  34. fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  35. // NSString -> NSDate
  36. NSDate *createdAtDate = [fmt dateFromString:createdAtString];
  37.  
  38. // 计算createdAtDate和nowDate的时间间隔
  39. NSCalendar *calendar = [NSCalendar currentCalendar];
  40. NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  41. NSDateComponents *cmps = [calendar components:unit fromDate:createdAtDate toDate:nowDate options:];
  42. NSLog(@"%@", cmps);
  43. }
  44.  
  45. /**
  46. * 获得某个NSDate对象的所有日期元素 : 年月日时分秒
  47. */
  48. void get_components_of_date()
  49. {
  50. // 获得系统当前时间
  51. NSDate *nowDate = [NSDate date];
  52.  
  53. // 日历对象
  54. NSCalendar *calendar = [NSCalendar currentCalendar];
  55. NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
  56. NSDateComponents *cmps = [calendar components:unit fromDate:nowDate];
  57. NSLog(@"%@", cmps);
  58.  
  59. // NSInteger year = [calendar component:NSCalendarUnitYear fromDate:nowDate];
  60. // NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:nowDate];
  61. // NSLog(@"%zd %zd", year, minute);
  62.  
  63. // NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  64. // // 设置日期格式
  65. // fmt.dateFormat = @"HH";
  66. // NSString *string = [fmt stringFromDate:nowDate];
  67. // NSLog(@"%@", string);
  68. }
  69.  
  70. /**
  71. * 2个NSDate之间的时间差值
  72. */
  73. void date_interval()
  74. {
  75. // 获得系统当前时间
  76. NSDate *nowDate = [NSDate date];
  77.  
  78. // 服务器返回的时间字符串
  79. NSString *createdAtString = @"2015-10-16 10:49:54";
  80. // 创建一个日期格式化对象
  81. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  82. // 设置日期格式
  83. fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  84. // NSString -> NSDate
  85. NSDate *createdAtDate = [fmt dateFromString:createdAtString];
  86.  
  87. // 计算createdAtDate和nowDate之间相隔的秒数
  88. NSTimeInterval interval = [nowDate timeIntervalSinceDate:createdAtDate];
  89. NSLog(@"%f", interval);
  90. // 3600 / 60 = 60
  91. // 60 /60 = 1
  92. }
  93.  
  94. /**
  95. * timestamp -> NSDate
  96. */
  97. void fmt_timestamp_to_date3()
  98. {
  99. // 时间戳 : 从1970年1月1日0点0分0秒开始经历的毫秒数
  100. NSInteger timestamp = ;
  101.  
  102. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];
  103.  
  104. NSLog(@"%@", date);
  105. }
  106.  
  107. /**
  108. * NSString -> NSDate
  109. */
  110. void fmt_string_to_date2()
  111. {
  112. // 服务器返回的时间字符串
  113. NSString *string = @"Tue May 31 17:46:55 +0800 2011";
  114.  
  115. // 创建一个日期格式化对象
  116. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  117. // 解析欧美格式的日期字符串, 得设置语言类型为en_US
  118. fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  119.  
  120. // 设置日期格式
  121. fmt.dateFormat = @"EEE MMM dd HH:mm:ss ZZZZ yyyy";
  122.  
  123. NSDate *date = [fmt dateFromString:string];
  124.  
  125. NSLog(@"%@", date);
  126. }
  127.  
  128. /**
  129. * NSString -> NSDate
  130. */
  131. void fmt_string_to_date()
  132. {
  133. // 服务器返回的时间字符串
  134. NSString *string = @"2015-10-16 10:49:54";
  135.  
  136. // 创建一个日期格式化对象
  137. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  138.  
  139. // 设置日期格式
  140. fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  141.  
  142. NSDate *date = [fmt dateFromString:string];
  143.  
  144. NSLog(@"%@", date);
  145. }
  146.  
  147. /**
  148. * NSDate -> NSString
  149. */
  150. void fmt_date_to_string()
  151. {
  152. // 获得系统当前时间
  153. NSDate *now = [NSDate date];
  154.  
  155. // 创建一个日期格式化对象
  156. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  157.  
  158. // 设置日期格式 : 2015年10月10日 15时56分30秒
  159. // 年(year) : y
  160. // 月(month) : M
  161. // 日(day) : d
  162. // 时(hour) : H(24小时制)\h(12小时制)
  163. // 分(minute) : m
  164. // 秒(second) : s
  165. // 时区(zone) : Z
  166.  
  167. fmt.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒";
  168.  
  169. // NSDate -> NSString
  170. NSString *nowString = [fmt stringFromDate:now];
  171.  
  172. NSLog(@"----- %@", nowString);
  173. }

NSDate 格式化 及 互转的更多相关文章

  1. NSDate 格式化 NSDate to NSString

    NSLog(@"%@",[NSDate stringFromDate:[NSDate date] withFormat:@"yyyyMMdd__HH_mm_ss_zzz& ...

  2. NSDate 格式化含有毫秒

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; 版权声明:本文为博主原创文章,未经博主允许不得转载.

  3. iOS中NSDate常用转换操作整合

    //当前时间格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss + (NSString *)getCurrentDataWithDateFormate:(NSString *)format ...

  4. oracle常规使用(一)

    目录 特殊sql distinct 项目中遇到表中无主键,但是某个字段不能重复. 需要匹配id串里的内容 批量更新,但是批量成功返回的是-1 时间格式化 行列互转 应用场景 列转行 总结 oracle ...

  5. 时间戳和LocalDateTime和Date互转和格式化

    一 前言 续上篇java8在日常开发中使用LocalDate和LocalTime[https://blog.csdn.net/youku1327/article/details/102771936]中 ...

  6. iOS NSDate获取当前时间并格式化

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM- ...

  7. day04-交互、格式化输出及基本运算符

    目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...

  8. NSString 常见数据类型转换:转NSInteger , NSDate(互转)

    1. NSString转NSInteger, 转int (float, double类似 ) 1.1正常情况 , NSString所包含内容确能转化为int的类型 NSString *sNumber ...

  9. DAY04 与用户交 互格式化输出与运算符

    与用户交互 输入: input # python2与python3的区别 # python3 res = input('please in put your username>>>& ...

随机推荐

  1. BackboneJS and UnderscoreJS

     介绍 来自API(backbone能做什么?) 当我们开发含有大量Javascript的web应用程序时,首先你需要做的事情之一便是停止向DOM对象附加数据. 通过复杂多变的jQuery选择符和回调 ...

  2. Detectron:Pytorch-Caffe2-Detectron的一些跟进

            pytorch官网:http://pytorch.org/上只有PyTroch的ubuntu和Mac版本,赤裸裸地歧视了一把Windows低端用户. 1. Caffe源码:Caffe源 ...

  3. (转)淘淘商城系列——Solr的安装

    http://blog.csdn.net/yerenyuan_pku/article/details/72874134 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service ...

  4. MFC_2.10选项卡控件的封装

    选项卡控件的封装 1.新建默认MFC项目 2.添加资源Dialog,属性style改child,边框改none,添加类取名CMyDialog1: 同理,CMyDialog2: 3.类向导,添加MFC类 ...

  5. 梦想CAD控件安卓界面控制

    CAD控件界面上所有元素都可以控制显示或隐藏,下面将逐一介绍详细用法. 设置工具文件 MxFunction.setToolFile 设置工具文件.详细说明如下: 参数 说明 String sFile ...

  6. 梦想CAD控件 2018.10.15更新

    下载地址: http://www.mxdraw.com/ndetail_10105.html 1. 完善com接口的ToCurves函数,转换CAD文字,多行文字到曲线 2. 修改DrawImage接 ...

  7. day02python

    ''' 列表 定义:在[]内,可以存放多个任意类型的值,并以逗号隔开. 一般用于存放学生的爱好,课堂的周期等等... ''' students=['钱垚','李小龙','张全蛋','赵铁柱'] pri ...

  8. Linux安装redis并且连接内网的redis

    1.安装redis步骤 1.首先准备工作  [root@10-100-14-130 ~]# yum install gcc-c++   yum install wget 2.推荐进入到linux路径/ ...

  9. iOS的影片播放 MediaPlayer 和 AVPlayer

    在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因為很方便使用,所以就一直使用下去.但是隨著客戶的要求越來越嚴苛,尤其是過場動畫或互動效果上的表 ...

  10. LeetCode_16 3SumCloest

    3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...