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. 深度学习Matlab DeepLearningToolBox 工具包最常见错误解决办法\

    deeplearningtoolbox  下载链接github : https://github.com/rasmusbergpalm/DeepLearnToolbox,只需要解压到matlab当前工 ...

  2. 使用Python扫描端口情况

    #!/usr/bin/python# -*- coding:utf8 -*-# Python:          2.7.8# Platform:        Windows# Authro:    ...

  3. [Node.js] Scraping Dynamic JavaScript Websites with Nightmare

    Many websites have more than just simple static content. Dynamic content which is rendered by JavaSc ...

  4. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  5. android 54 播放音视频

    mainActivity: package com.sxt.day07_09; import java.util.ArrayList; import java.util.HashMap; import ...

  6. mybatis0208 缓存

    查询缓存 1.1缓存的意义 数据在磁盘会有一个IO,高并发读取效率就很低,将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效 ...

  7. Systemtap kernel.trace("*") events source code

    http://blog.163.com/digoal@126/blog/static/16387704020131014562216/

  8. Annotation Type @bean,@Import,@configuration使用--官方文档

    @Target(value={METHOD,ANNOTATION_TYPE}) @Retention(value=RUNTIME) @Documented public @interface Bean ...

  9. WebLogic Server的单点登陆功能--转载

    在WebLogic 8.1最新的 SP4版本中,最引人注目的要算是在安全方面,提供了用于和Microsoft Windows客户端进行Single Sign-On的Single Pass Negoti ...

  10. Java多线程之释放对象的锁

          由于等待一个锁定线程只有在获得这把锁之后,才能恢复运行,所以让持有锁的线程在不需要锁的时候及时释放锁是很重要的.在以下情况下,持有锁的线程会释放锁: 1. 执行完同步代码块. 2. 在执行 ...