// 当前时间创建NSDate
NSDate *myDate = [NSDate date];
NSLog(@"myDate = %@",myDate);
//从现在开始的24小时
NSTimeInterval secondsPerDay = **;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"myDate = %@",tomorrow);
//根据已有日期创建日期
NSTimeInterval secondsPerDay1 = **;
NSDate *now = [NSDate date];
NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];
NSLog(@"yesterDay = %@",yesterDay);
//比较日期
BOOL sameDate = [now isEqualToDate:yesterDay];
NSLog(@"sameDate = %lu",sameDate);
//获取较早的日期
NSDate *earlierDate = [yesterDay earlierDate:now];
NSLog(@"earlierDate = %@",earlierDate);
//较晚的日期
NSDate *laterDate = [yesterDay laterDate:now];
NSLog(@"laterDate = %@",laterDate);
//两个日期之间相隔多少秒
NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];
NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates);
//通过NSCALENDAR类来创建日期
NSDateComponents *comp = [[NSDateComponentsalloc]init];
[comp setMonth:];
[comp setDay:];
[comp setYear:];
NSCalendar *myCal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *myDate1 = [myCal dateFromComponents:comp];
NSLog(@"myDate1 = %@",myDate1);
//从已有日期获取日期
unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents *comp1 = [myCal components:units fromDate:now];
NSInteger month = [comp1 month];
NSInteger year = [comp1 year];
NSInteger day = [comp1 day];
//NSDateFormatter实现日期的输出
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateStyle:NSDateFormatterFullStyle];
//直接输出的话是机器码
//或者是手动设置样式
[formatter setDateFormat:@"yyyy-mm-dd"];
NSString *string = [formatter stringFromDate:now];
NSLog(@"string = %@",string);
NSLog(@"formater = %@",formatter);
//获取日期格式对象
- (NSDateFormatter *)dateFormatter {
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}

NSDate常用的日期操作的更多相关文章

  1. Java【常用的日期操作】

    目录 1.设置时间 2.获取年月日时分秒 3.通过运算获取时间 4.和Date类转换 5.格式化时间 6.新功能LocalDate:当前日期格式化 7.示例 java.util.Calendar 类是 ...

  2. Java常用日期操作

    对java中常用的日期操作进行整理. 1.日期格式化 /* * 日期格式化类(必须掌握) * API: * G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Mo ...

  3. NSDate 总结日期操作

    IOS Object-c NSDate总结日期操作 //NSDate //1, 创建NSDate对象 NSDate *nowDate = [NSDate date]; NSLog(@"%@& ...

  4. java日期操作常用工具

    java日期操作常用工具 package com..util; import java.sql.Timestamp; import java.text.SimpleDateFormat; import ...

  5. JAVASE02-Unit03: 日期操作 、 集合框架

    Unit03: 日期操作 . 集合框架 java.util.Date package day03; import java.util.Date; /** * java.util.Date * Date ...

  6. java日期操作大全

    摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个:  取得指定月份的第一天与取得指定月份的最后一天  http ...

  7. js中时间戳与日期转换-js日期操作

    常用的一些日期操作. 用js获取一个时间戳. <script type="text/javascript"> var date = new Date();//当前时间 ...

  8. Java中的日期操作

    在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...

  9. NSDate常用代码范例

    NSDate常用代码范例 NSDate类用于保存时间值,同时提供了一些方法来处理一些基于秒级别时差(Time Interval)运算和日期之间的早晚比较等. 1. 创建或初始化可用以下方法 用于创建N ...

随机推荐

  1. PHP基础2--基本语法

    主要: 标记符,注释 变量 常量 数据类型 运算符 流程控制 标记符,注释 4种标记符号: 1.  默认形式:  <?php    php语句      ?> 如果<?php ... ...

  2. spark sql cache时发现的空字符串问题

    博客园首发,转帖请注明地址:https://www.cnblogs.com/tzxxh/p/10267202.html 图一 图1未做cache,直接过滤expression列的 null 和空字符串 ...

  3. java中stream部分笔记

    Stream流表面上看起来与集合类似,允许你转换和检索数据.然而,两者却有显著的不同1.流不存储元素.它们存储在底层的集合或者按需生成2.流操作不改变他们的源数据.例如filter方法不会从一个新流中 ...

  4. 成都Uber优步司机奖励政策(2月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. 宁波Uber优步司机奖励政策(8月10号-16号)

    本周奖励: 8月10日-8月16日: 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://w ...

  6. JavaScript---复选框全选的多种实现

    <script language=javascript> //第一种方法 function selectall1() {    var a = document.getElementsBy ...

  7. 【完美解决】Spark-SQL、Hive多 Metastore、多后端、多库

    [完美解决]Spark-SQL.Hive多 Metastore.多后端.多库 [完美解决]Spark-SQL.Hive多 Metastore.多后端.多库 SparkSQL 支持同时连接多种 Meta ...

  8. nginx 路由配置

    nginx中location对url匹配: 语法:location [=|~|~*|^~] /uri/ { … } 当匹配中符合条件的location,则执行内部指令:如果使用正则表达式,必须使用~* ...

  9. libevent学习六(Connect listeners )

      创建与释放 //backlog需要查询平台说明,在linux2.2以后 backlog就变成了已完成连接但未accept的队列的最大值(原来是处于syn状态的,现在换成sysctl 控制的参数tc ...

  10. nginx支持php配置

    location / { root /wwwroot/phptest; index index.html index.htm index.php; } location ~ \.(php|php5)$ ...