美文网首页
Date时间基础记录

Date时间基础记录

作者: 王云晨 | 来源:发表于2017-04-18 15:30 被阅读0次

一.时间戳

二.Date的基础介绍

三.NSCalendar的基础介绍


一.时间戳

  • 时间戳:从1970年1月1号 00:00:00开始走过的毫秒数。IOS端默认生成的时间戳是10位的,如果服务器返回的13位需要除以1000例如:
//字符串 -> 时间戳
NSString * timeStampString = @"1423189125435"
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue] / 1000];
  • 时间戳转Date
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];

二.Date的基础介绍

1.时间和时区 (北京为东8区)
  • 创建date对象即时间对象
    NSDate *dats = [[NSDate alloc]init];
  • 获取当前时间(本初子午线的时间)
    NSDate *date = [NSDate date];
  • 获取当前所在地区的时区
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
  • 获取当前时区和指定时间的时差
NSTimeInterval seconds = [zone secondsFromGMTForDate:date];
 NSDate *nowDate = [date dateByAddingTimeInterval:secend];
2.字符串于Date的相互转化
  • 字符串-> Date
 NSString *createdString = @"2018-11-20";
//时间格式化对象
NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
dateformate.dateFormat = @"yyyy-MM-dd";
//会自动减去8个小时
NSDate *creatDate = [dateformate dateFromString:createdString];
NSLog(@"%@",creatDate);
  • Date ->字符串
    NSDate *nowdate = [NSDate date]; NSString *nowstring = [dateformate stringFromDate:nowdate];

总结:Date 和字符串相互转换需要一个桥梁NSDateFormatter,NSDateFormatter 的格式有很多比如:yyyy/MM/dd HH/mm/ss 、yyyy年MM月dd日 HH时mm分ss秒、yyyy/MM/dd等。

3.通过Date比较两时间 (NSComparisonResult)
    NSString *createdString = @"2018-11-20";
    NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
    dateformate.dateFormat = @"yyyy-MM-dd";//自动减去8个小时
    NSDate *creatDate = [dateformate dateFromString:createdString];
    NSLog(@"%@",creatDate);
    NSDate *nowdate = [NSDate date];
    NSComparisonResult result = [creatDate compare:nowdate];
    if (result == NSOrderedAscending) {
        NSLog(@"nowdate>creatDate");
    }
    else if(result == NSOrderedDescending)
    {
       NSLog(@"creatDate>nowdate");
    }
    else if(result == NSOrderedSame)
    {
        NSLog(@"creatDate=nowdate");
    }
4.时间的加减 (通过时间戳)
NSDate*nowDate = [NSDate date];
NSTimeInterval  interval =24*60*60*1; //1:天数
NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:+interval];//加一天
NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:-interval];//减一天

三.NSCalendar的基础介绍

NSDate * date  = [NSDate date];

NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
// NSDateComponent 可以获得日期的详细信息,即日期的组成
NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];

NSLog(@"年 = year = %ld",comps.year);
NSLog(@"月 = month = %ld",comps.month);
NSLog(@"日 = day = %ld",comps.day);
NSLog(@"时 = hour = %ld",comps.hour);
NSLog(@"分 = minute = %ld",comps.minute);
NSLog(@"秒 = second = %ld",comps.second);
NSLog(@"星期 =weekDay = %ld ",comps.weekday);// 从周日开始算的
  • 通过NSCalendar日期比较
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法
    NSDate * currentDate = [NSDate date]; // 这个日期可以你自己给定
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
    inUnit: NSCalendarUnitMonth
    forDate:currentDate];
    NSLog(@"%ld",range.length);

     //日期比较
     NSDateComponents *cmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSince1970:0] toDate:[NSDate date] options:0];
     NSLog(@"%li",(long)cmps.year);
    

NSCalendarUnitYear这个参数可选:

   NSCalendarUnitYear            
   NSCalendarUnitMonth          
   NSCalendarUnitDay             
   NSCalendarUnitHour            
   NSCalendarUnitMinute      
   NSCalendarUnitSecond            
   NSCalendarUnitWeekday         

最后写一个小的方法,项目中经常用到字符串类型的时间大小对比

//对比两个时间大小。
-(BOOL)comparedate:(NSString *)firstTime secendtime:(NSString *)secendTime andNSDateFormatter:(NSDateFormatter *)formatter
{
    NSDateFormatter *nowformatter;
    if (formatter) {
        nowformatter = formatter;
        
    }
    else
    {
        nowformatter = [[NSDateFormatter  alloc]init];
        nowformatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    }
    
    NSDate *firestDate =[nowformatter dateFromString:firstTime];
    NSDate *secendDate = [nowformatter dateFromString:secendTime];
    NSComparisonResult result = [firestDate compare:secendDate];
    if (result == NSOrderedAscending) {
        return NO;
    }
    else if(result  == NSOrderedDescending)
    {
        return YES;
    }
    else
    {
        return YES;
    }
}

以上都是一些基础知识,方便以后查阅。

相关文章

  • Date时间基础记录

    一.时间戳 二.Date的基础介绍 三.NSCalendar的基础介绍 一.时间戳 时间戳:从1970年1月1号 ...

  • JavaScript ☞ day2

    JavaScript基础学习笔记之JavaScript提升 了解时间 Date Date对象的方法 Date对象间...

  • js笔记十八之Date日期

    Date日期操作基础讲解 Date是日期类,通过它可以对时间进行处理

  • Linux查看系统时间(纳秒级)

    命令 查看设置时间命令:date ——按指定格式显示时间,或者设置系统时间。 基础用法 在命令行下输入date,...

  • JavaScript 08 Date

    一、Date对象 什么是Date对象? Date对象用于处理日期和时间,Date对象记录着从1970年1月1日00...

  • DataPicker

    Android基础:Date & Time组件(上)Android基础:Date & Time组件(下)

  • linux的文件类型

    基础命令: date: date+选项+参数显示 FORHAT:格式符号 %D %F %T date【MMDDhh...

  • 2019-06-12

    js基础知识: 时钟: var now = new Date();弹出美式时间 常用写时间样式: oBox.inn...

  • Mysql常用SQL语句收集

    mysql常用sql语句收集 基础篇 //查询时间,友好提示 $sql = "select date_format...

  • 一学Linux之date和cal

    基础命令 date命令 显示日期与时间的命令。 cal命令 显示日历的命令。

网友评论

      本文标题:Date时间基础记录

      本文链接:https://www.haomeiwen.com/subject/pzifzttx.html