iOS日期的簡單應用

字符串轉(zhuǎn)化為日期:

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];

國際時間轉(zhuǎn)化:

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];

將現(xiàn)在時間轉(zhuǎn)化為字符串:

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];

和手機現(xiàn)在時間比較:

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(@"現(xiàn)在時間");    
}else if (result == NSOrderedDescending){         
   NSLog(@"過去時間");
}

距離現(xiàn)在多少秒:

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
    // 是否在一個星期之內(nèi)
    - (BOOL)isDateInWeekend:(NSDate *)date
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,679評論 0 4
  • 你的難可以告訴誰? 難即難過、難處、困難、傷心、無奈、無助…… 它們是負面情緒,是人們常說的負能...
    黃希兒閱讀 335評論 7 6
  • 我們都喜歡在云霞收練之前遐想 然后期待恒久的星光 可是當?shù)诙斓某兑粊?除了漫身的咸香 對歲月又別無他方
    我是秀啊閱讀 379評論 3 4
  • 這種感覺真的很糟 因為一句話就高興半天 發(fā)現(xiàn)了一件事就心涼半截 喜歡不能太明顯 不然不值錢.
    冷漠無情的犬哥閱讀 160評論 0 1
  • 首飾疊戴這個風潮從去年就開始慢慢吹向時尚界,2016年的春天,這股風沒有要消退的態(tài)勢,反而愈演愈烈。一條項鏈,一款...
    茉客閱讀 1,532評論 0 0

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