时间日期处理

  • NSString -> NSDate 处理国内时间格式
void string2date(){
    //国内化时间
    NSString *string = @"2017-11-14 16:14:00";
   
    //1.将NSDate转为NSString
    //2.将NSString转为NSDate
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [fmt dateFromString:string];
    NSLog(@"%@--", date);
}
  • NSString -> NSDate 处理国际时间格式
void string2date2(){
    //国际化时间
    NSString *string = @"Tue Nov 24 16:26:26 2017";
    
    //1.将NSDate转为NSString
    //2.将NSString转为NSDate
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss yyyy";
    //设置语言区域
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    NSDate *date = [fmt dateFromString:string];

    NSLog(@"%@", date);
}
  • NSString -> NSDate 处理时间戳格式
void string2date3(){
    //时间字符串 - 时间戳
    NSString *string = @"1432182932982";
    //毫秒转换为秒
    NSTimeInterval second = string.longLongValue / 1000.0;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
    
    NSLog(@"%@",date);
}
  • NSDate -> NSString
void date2string(){
    NSDate *date = [NSDate date];
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *string = [fmt stringFromDate:date];
    
    NSLog(@"%@", string);
}
  • 获取某个日期元素
void getComponentsOfDate(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串转为date对象
    NSDate *date = [fmt dateFromString:string];
    
    //利用NSCalendar处理日期
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:date];
    
    NSLog(@"%zd", month);
}
  • 获取某些日期元素
#define iOS(version) [UIDevice currentDevice].systemVersion.doubleValue >= (version)

void getComponentsOfDate2(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串转为date对象
    NSDate *date = [fmt dateFromString:string];
    
    //NSCalendar系统适配
    NSCalendar *calendar = nil;
//    if (iOS(8.0)) {
//        calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
//    }else{
//        calendar = [NSCalendar currentCalendar];
//    }
    
    //利用NSCalendar生成日期
    //[calendar dateWithEra:<#(NSInteger)#> year:<#(NSInteger)#> month:<#(NSInteger)#> day:<#(NSInteger)#> hour:<#(NSInteger)#> minute:<#(NSInteger)#> second:<#(NSInteger)#> nanosecond:<#(NSInteger)#>]
    
    NSCalendarUnit unit = NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *cmps = [calendar components:unit fromDate:date];
    
    NSLog(@"%@", cmps);
}
  • 日期的比较方法(粗略比较大小)
void compareDate(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串转为date对象
    NSDate *date = [fmt dateFromString:string];
    
    NSDate *nowDate = [NSDate date];
    
    NSComparisonResult result = [date compare:nowDate];
    if (result == NSOrderedAscending) {
        //升序
    }else if(result == NSOrderedDescending){
        //降序
    }else{
        //same
    }
    
    //比较字符串
    NSString *str1 = @"abc";
    NSString *str2 = @"aBc";
    [str1 compare:str2];
    
}
  • 日期的比较方法(比较相差秒数)
void compareDate2(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串转为date对象
    NSDate *date = [fmt dateFromString:string];
    
    NSDate *nowDate = [NSDate date];
    
    //获取时间间隔的秒
    NSTimeInterval interval = [date timeIntervalSinceDate:nowDate];
    
    NSLog(@"%f", interval);
}
  • 通过 NSDateComponents 获取日期比较的结果
void compareDate3(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串转为date对象
    NSDate *date = [fmt dateFromString:string];
    
    NSDate *nowDate = [NSDate date];
    
    NSCalendar *calendar = nil;
    //系统适配
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }else{
        calendar = [NSCalendar currentCalendar];
    }
    NSCalendarUnit unit = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour;
    NSDateComponents *cmps = [calendar components:unit fromDate:nowDate toDate:date options:0];
    
    NSLog(@"%@",cmps);
}
  • iOS 8.0 后一些新的比较方法
void other(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //设置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串转为date对象
    NSDate *date = [fmt dateFromString:string];

    NSCalendar *calendar = nil;
    //系统适配
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }else{
        calendar = [NSCalendar currentCalendar];
    }
    //其他的比较方法
    [calendar isDateInToday:date];
}

你可能感兴趣的:(时间日期处理)