時間日期處理

  • 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)系作者
  • 序言:七十年代末罐盔,一起剝皮案震驚了整個濱河市担平,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌芒率,老刑警劉巖囤耳,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異偶芍,居然都是意外死亡充择,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門匪蟀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來椎麦,“玉大人,你說我怎么就攤上這事材彪」劭妫” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵段化,是天一觀的道長嘁捷。 經(jīng)常有香客問我,道長显熏,這世上最難降的妖魔是什么雄嚣? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮佃延,結(jié)果婚禮上现诀,老公的妹妹穿的比我還像新娘。我一直安慰自己履肃,他們只是感情好仔沿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著尺棋,像睡著了一般封锉。 火紅的嫁衣襯著肌膚如雪绵跷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天成福,我揣著相機(jī)與錄音碾局,去河邊找鬼。 笑死奴艾,一個胖子當(dāng)著我的面吹牛净当,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蕴潦,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼像啼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了潭苞?” 一聲冷哼從身側(cè)響起忽冻,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎此疹,沒想到半個月后僧诚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蝗碎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年湖笨,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蹦骑。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡赶么,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出脊串,到底是詐尸還是另有隱情,我是刑警寧澤清钥,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布琼锋,位于F島的核電站,受9級特大地震影響祟昭,放射性物質(zhì)發(fā)生泄漏缕坎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一篡悟、第九天 我趴在偏房一處隱蔽的房頂上張望谜叹。 院中可真熱鬧,春花似錦搬葬、人聲如沸荷腊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽女仰。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間疾忍,已是汗流浹背乔外。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留一罩,地道東北人杨幼。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像聂渊,于是被迫代替她去往敵國和親差购。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容