Date时间基础记录

一.时间戳

二.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;
    }
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS开发中,经常会遇到各种各样的时间问题,8小时时差,时间戳,求时间间隔,农历等等。解决办法网上比比皆是,但大...
    真巧了_嘿阅读 2,836评论 0 7
  • 作品链接:http://www.jianshu.com/users/1e0f5e6f73f6/top_articl...
    打电话记错号码的人阅读 29,331评论 0 26
  • ######先说下需求:选择日期弹出日历(跟途牛,携程等差不多就行。。。行) 初识NSCalendar到写完日历的...
    只是个少年阅读 1,097评论 0 0
  • iOS开发中,经常会遇到各种各样的时间问题,8小时时差,时间戳,求时间间隔,农历等等。解决办法网上比比皆是,但大多...
    小李龍彪阅读 6,502评论 1 6
  • 你发现了吗 所有的作品 并不是 一开始就很好的 比如歌要听很多遍 比如诗要读很多遍 比如画要不断不断 盯着看很久很...
    天野丢阅读 194评论 4 3