iOS日期的简单应用

作者: 芝麻绿豆 | 来源:发表于2015-11-21 20:24 被阅读421次

    字符串转化为日期:

    NSString *strDate = @"2015-03-20 12:45:54";    
    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [frm dateFromString:strDate];
    

    国际时间转化:

    NSString *str = @"Tue May 21 17:40:55 +0800 2014";    
    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";    
    frm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    NSDate *date = [frm dateFromString:str];
    

    将现在时间转化为字符串:

    NSDate *date = [[NSDate alloc] init];   
    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *str = [frm stringFromDate:date];
    

    获取日期中的某个时间属性:

    NSString *string = @"2014-01-20 09:10:05";    
    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
    NSDate *date = [frm dateFromString:string];       
    NSCalendar *cld = [NSCalendar currentCalendar];    
    NSInteger month = [cld component:NSCalendarUnitMonth fromDate:date];    
    NSInteger hour = [cld component:NSCalendarUnitHour fromDate:date];
    NSInteger day = [cld component:NSCalendarUnitDay fromDate:date];
    
    NSString *string = @"2014-01-20 09:10:05";    
    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
    NSDate *date = [frm dateFromString:string];    
    NSCalendar *cld = [NSCalendar currentCalendar];    
    NSCalendarUnit unit = NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    NSDateComponents *dateCom = [cld components:unit fromDate:date];
    

    和手机现在时间比较:

    NSString *string = @"2019-01-20 09:10:05";    
    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
    NSDate *creatdate = [frm dateFromString:string];    
    NSDate *nowDate = [NSDate date];    
    NSComparisonResult result = [nowDate compare:creatdate];    
    /*     
    NSOrderedAscending = -1L, 升序     
    NSOrderedSame,     
    NSOrderedDescending 降序     
    */   
    if (result == NSOrderedAscending) {        
       NSLog(@"未来时间");    
    }else if (result == NSOrderedSame){          
       NSLog(@"现在时间");    
    }else if (result == NSOrderedDescending){         
       NSLog(@"过去时间");
    }
    

    距离现在多少秒:

    NSString *string = @"2015-11-21 19:38:05";   
    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
    NSDate *creatdate = [frm dateFromString:string];
    NSTimeInterval interval = [creatdate timeIntervalSinceNow];
    

    两个时间比较:

    NSDateFormatter *frm = [[NSDateFormatter alloc] init];    
    frm.dateFormat = @"yyyy-MM-dd HH:mm:ss";    
    NSString *createdAtString = @"2015-11-01 09:10:05";    
    NSDate *createdAtDate = [frm dateFromString:createdAtString];       
    NSString *otherString = @"2015-10-31 08:56:45";    
    NSDate *otherDate = [frm dateFromString:otherString];       
    NSCalendar *calendar = nil;    
    // iOS8.0之后使用    
    if ([NSCalendar resolveInstanceMethod:@selector(calendarWithIdentifier:)]) { 
           calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian ];    
    }else{           
           calendar = [NSCalendar currentCalendar];    
    }    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;    
    NSDateComponents *com = [calendar components:unit fromDate:createdAtDate toDate:otherDate options:0];
    NSLog(@"%@",com);
    

    判断是否是今天:

    NSCalendar *calendar = nil;   
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {  
          calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];   
     } else {   
         calendar = [NSCalendar currentCalendar];  
      }     
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];    
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";   
    NSLog(@"%zd", [calendar isDateInToday:[fmt dateFromString:@"2015-11-10 01:09:56"]]);
    
        // 是否是昨天
        - (BOOL)isDateInYesterday:(NSDate *)date 
        // 是否是明天
        - (BOOL)isDateInTomorrow:(NSDate *)date
        // 是否在一个星期之内
        - (BOOL)isDateInWeekend:(NSDate *)date
    

    相关文章

      网友评论

      本文标题:iOS日期的简单应用

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