IOS Object-c NSDate总结日期操作

//NSDate

//1, 创建NSDate对象

NSDate *nowDate = [NSDate date];

NSLog(@"%@",nowDate);

//2, 创建明天现在的时间

NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:24*3600];

NSLog(@"%@",tomorrow);

//3, 创建昨天现在的时间

NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*3600];

NSLog(@"%@",yesterday);

//4, 间隔

NSTimeInterval interval = [tomorrow timeIntervalSinceDate:yesterday];

NSLog(@"间隔%.0f",interval/3600);

//计算当前时间和⼀一个固定时间的差值,如果差值在60秒内,输出“刚 刚”,如果在60秒外3600秒内,输出“xx分钟前”,如果3600秒外, 3600*24秒内,输出“xx⼩小时前”。

NSDate *t1 = [NSDate dateWithTimeIntervalSinceNow:10];

//

NSTimeInterval time2 = [t1 timeIntervalSinceNow];

if (time2 <= 60) {

NSLog(@"刚刚");

}else if (time2 <= 3600 && time2 > 60){

NSLog(@"%f",time2/3600);

}else{

NSLog(@"%f",time2);

}

//创建时间格式化类

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//设置样式

[dateFormatter setDateFormat:@"yyyy年MM月dd日 hh时mm分ss秒"];

//

NSString *dateString = [dateFormatter stringFromDate:nowDate ];

NSLog(@"%@",dateString);

1 // 当前时间创建NSDate

NSDate *myDate = [NSDate date];

NSLog(@"myDate = %@",myDate);

2 //从现在开始的24小时

NSTimeInterval secondsPerDay = 24*60*60;

NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

NSLog(@"myDate = %@",tomorrow);

3//根据已有日期创建日期

NSTimeInterval secondsPerDay1 = 24*60*60;

NSDate *now = [NSDate date];

NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];

NSLog(@"yesterDay = %@",yesterDay);

4//比较日期

BOOL sameDate = [now isEqualToDate:yesterDay];

NSLog(@"sameDate = %lu",sameDate);

4.1//获取较早的日期

NSDate *earlierDate = [yesterDay earlierDate:now];

NSLog(@"earlierDate  = %@",earlierDate);

4.2//较晚的日期

NSDate *laterDate = [yesterDay laterDate:now];

NSLog(@"laterDate  = %@",laterDate);

//两个日期之间相隔多少秒

NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];

NSLog(@"secondsBetweenDates=  %lf",secondsBetweenDates);

//通过NSCALENDAR类来创建日期

NSDateComponents *comp = [[NSDateComponentsalloc]init];

[comp setMonth:06];

[comp setDay:01];

[comp setYear:2001];

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 字符串操作和日期操作

    一.字符串操作 创建字符串 String s2 = new String("Hello World"); String s1 = "Hello World"; ...

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

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

  3. Lua库之时间和日期操作

    Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...

  4. java日期操作大全

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

  5. oracle日期操作

    日期操作:ADD_MONTHS(date,i) 作用 返回在自定日期上添加的月份 i是整数 如果i是小数,则截取整数部分 i是负数 原有日期减去相应部分 例子: SQL> select add_ ...

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

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

  7. Java中的日期操作

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

  8. js日期操作

    1.最基本的日期操作 var mydate = new Date(); set/get   FullYear,Month,Date,Hour,Minutes,Second可以随意拼接 toLocale ...

  9. JS常见操作,日期操作,字符串操作,表单验证等

    复制代码 //第一篇博文,希望大家多多支持 /***** BasePage.js 公共的 脚本文件 部分方法需引用jquery库 *****/ //#region 日期操作 //字符串转化为时间. f ...

随机推荐

  1. Raspberry Pi + 3个USB摄像头 + Motion(简易监控设备配置记录1——介绍以及安装) 分类: Raspberry Pi 服务器搭建 2015-04-12 19:21 226人阅读 评论(0) 收藏

    参考: Debian官网链接 Motion官网链接 首先,参见Debian官网链接对Motion的介绍,网页中包含了所有相关依赖包,请首先确保这些依赖包的安装. Motion介绍 摘出对Motion的 ...

  2. spring session工程发布--一种新的管理httpsession的方法

    官方文档:http://spring.io/blog/2014/07/08/spring-session-1-0-0-m1-released 1. 优点: This project provides ...

  3. windows和linux双系统删除linux

    装了Windows和linux双系统的朋友,在后期要删除linux是个比较头痛的问题,因为MBR已经被linux接管,本文的目的是如何在windows 和linux双系统下,简单,完美地卸载linux ...

  4. ARCGIS二维三维导航

    在使用代码前需要先安装arcgis10.0    或者10.1都可以    不过本人建议初学者安装10.0比较容易安装.. 安装方式和二维三维地图的加载网上都有,就不在此一一赘述了. 先从基本的功能开 ...

  5. Weex 初始

    1.一旦数据和模板绑定,数据的变化会立即体现在前台的变化 <template> <container> <text style="font-size: {{si ...

  6. php100视频原始地址列表整理:

    php100视频原始地址列表整理: 教程名称 . 1:环境配置与代码调试 2:PHP的数据类型与源码调试 3:常用PHP运算类型介绍与应用 4: PHP条件语句介绍与应用 5:PHP循环语句的介绍与应 ...

  7. http错误代码含义大全详解

    http 错误代码表 所有 HTTP 状态代码及其定义.  代码  指示  2xx  成功  200  正常:请求已完成.  201  正常:紧接 POST 命令.  202  正常:已接受用于处理, ...

  8. ajax 操作全局监测,用户session失效

    jQuery(function ($) { // 备份jquery的ajax方法 var _ajax = $.ajax; // 重写ajax方法,先判断登录在执行success函数 $.ajax = ...

  9. PSD 转化成 HTML

    一般情况下,网页设计制作完成的工作实际是:psd 效果图 转成 html+CSS 的模板页面,一般情况下,我们会拿到美工的 psd,不同的人会有不同的做法: 打开fireworks将图片切割导出为ht ...

  10. 【HDU4391】【块状链表】Paint The Wall

    Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...