時間日期處理

  • NSString -> NSDate 處理國內(nèi)時間格式
void string2date(){
    //國內(nèi)化時間
    NSString *string = @"2017-11-14 16:14:00";
   
    //1.將NSDate轉(zhuǎn)為NSString
    //2.將NSString轉(zhuǎn)為NSDate
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //設(shè)置日期格式
    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轉(zhuǎn)為NSString
    //2.將NSString轉(zhuǎn)為NSDate
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //設(shè)置日期格式
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss yyyy";
    //設(shè)置語言區(qū)域
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    NSDate *date = [fmt dateFromString:string];

    NSLog(@"%@", date);
}
  • NSString -> NSDate 處理時間戳格式
void string2date3(){
    //時間字符串 - 時間戳
    NSString *string = @"1432182932982";
    //毫秒轉(zhuǎn)換為秒
    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];
    
    //設(shè)置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串轉(zhuǎn)為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];
    
    //設(shè)置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串轉(zhuǎn)為date對象
    NSDate *date = [fmt dateFromString:string];
    
    //NSCalendar系統(tǒng)適配
    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];
    
    //設(shè)置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串轉(zhuǎn)為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];
    
}
  • 日期的比較方法(比較相差秒數(shù))
void compareDate2(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //設(shè)置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串轉(zhuǎn)為date對象
    NSDate *date = [fmt dateFromString:string];
    
    NSDate *nowDate = [NSDate date];
    
    //獲取時間間隔的秒
    NSTimeInterval interval = [date timeIntervalSinceDate:nowDate];
    
    NSLog(@"%f", interval);
}
  • 通過 NSDateComponents 獲取日期比較的結(jié)果
void compareDate3(){
    NSString *string = @"2017-11-14 16:14:00";
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    //設(shè)置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串轉(zhuǎn)為date對象
    NSDate *date = [fmt dateFromString:string];
    
    NSDate *nowDate = [NSDate date];
    
    NSCalendar *calendar = nil;
    //系統(tǒng)適配
    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];
    
    //設(shè)置日期格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    //字符串轉(zhuǎn)為date對象
    NSDate *date = [fmt dateFromString:string];

    NSCalendar *calendar = nil;
    //系統(tǒng)適配
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }else{
        calendar = [NSCalendar currentCalendar];
    }
    //其他的比較方法
    [calendar isDateInToday:date];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容