iOS關(guān)于時(shí)間的處理看我就夠了


一、字符串轉(zhuǎn)化成日期:

NSString *str = @"2017-10-21 23:00:11 +0800";

// 1.創(chuàng)建一個(gè)時(shí)間格式化對(duì)象

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

// 2.格式化對(duì)象的樣式

formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";

// 3.格式化讓字符串轉(zhuǎn)換成時(shí)間

NSDate *datestr = [formatter dateFromString:str];

// 4.自己給定的時(shí)間

NSDate *currentDate = datestr;

二、獲取當(dāng)月天數(shù):

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

//此處是查看當(dāng)前月的天數(shù)

NSDate *timeDate = [NSDate date];

NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit: NSMonthCalendarUnit forDate:timeDate];

NSInteger countInteger = range.length;

NSLog(@"當(dāng)月天數(shù)為:%lu",(unsigned long)countInteger);

三、獲取指定日期是周幾

NSDate *datestr = [NSDate date];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日歷的算法

NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:datestr];

// 1:周日,2:周一 ,3.周二 ? ? ...以此類推

NSLog(@"今天是:%@",@([comps weekday]));

四、判斷指定時(shí)間與當(dāng)前時(shí)間大小

// 時(shí)間字符串

NSString *starTime= @"2017-23-20 11:10:05";

NSDateFormatter *format = [[NSDateFormatter alloc] init];

format.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSDate *currentTime = [format dateFromString:starTime];

// 當(dāng)前時(shí)間

NSDate *nowTime = [NSDate date];

// 獲得比較結(jié)果(誰大誰小)

NSComparisonResult result = [nowTime compare:currentTime];

if (result == NSOrderedAscending) {

// 升序, 越往右邊越大

NSLog(@"創(chuàng)建時(shí)間大于當(dāng)前時(shí)間");

} else if (result == NSOrderedDescending) {

// 降序, 越往右邊越小

NSLog(@"創(chuàng)建時(shí)間小于當(dāng)前時(shí)間");

} else {

NSLog(@"創(chuàng)建時(shí)間等于當(dāng)前時(shí)間");

}

五、獲取年月日星期時(shí)分秒

NSDate* date? = [NSDate date];

NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

?// NSDateComponent 可以獲得日期的詳細(xì)信息,即日期的組成

NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];

? ? NSLog(@"年 = year = %ld",comps.year);

? ? NSLog(@"月 = month = %ld",comps.month);

? ? NSLog(@"日 = day = %ld",comps.day);

? ? NSLog(@"時(shí) = hour = %ld",comps.hour);

? ? NSLog(@"分 = minute = %ld",comps.minute);

? ? NSLog(@"秒 = second = %ld",comps.second);

? ? NSLog(@"星期 =weekDay = %ld ",comps.weekday);

//字符串類型? 轉(zhuǎn)化為? 時(shí)間

NSString *string = @"1481706995000";

NSTimeInterval time = [string doubleValue] ;

NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSLog(@"轉(zhuǎn)換的時(shí)間為:%@",[dateFormatter stringFromDate:date]);

輸出:

轉(zhuǎn)換的時(shí)間為:2016-12-14 17:56:35

記錄兩次請(qǐng)求前后時(shí)間差:

NSDate? *date1 = [NSDate date];

NSDate? *date2 = [NSDate date];

NSTimeInterval? a = [date2? timeIntervalSince1970]? -? [date1? timeIntervalSince1970];

NSLog(@"A與B之間相差%.3f秒",a);

時(shí)間戳? 時(shí)間 互換

//時(shí)間戳 -> 時(shí)間

NSDateFormatter *tmpFormatter = [[NSDateFormatter alloc] init];

[tmpFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

[tmpFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];

NSDate *tmpTimedate = [NSDate dateWithTimeIntervalSince1970:tmpTime];

_mStrTime = [tmpFormatter stringFromDate:tmpTimedate];

參考:

+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

[formatter setTimeZone:timeZone];

NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

NSLog(@"1296035591? = %@",confromTimesp);

NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

//NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);

return confromTimespStr;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 像小強(qiáng)一樣活著閱讀 3,744評(píng)論 6 16
  • ``` 時(shí)間和日歷的計(jì)算在iOS開發(fā)中經(jīng)??吹?,經(jīng)常看到大家在百度,開源中國等搜索這些答案。今天寫個(gè)簡(jiǎn)單的時(shí)間和日...
    昊楷閱讀 1,823評(píng)論 1 4
  • 在iOS開發(fā)中,經(jīng)常會(huì)遇到各種各樣的時(shí)間問題,8小時(shí)時(shí)差,時(shí)間戳,求時(shí)間間隔,農(nóng)歷等等。解決辦法網(wǎng)上比比皆是,但大...
    真巧了_嘿閱讀 2,939評(píng)論 0 7
  • iOS開發(fā)中,經(jīng)常會(huì)遇到各種各樣的時(shí)間問題,8小時(shí)時(shí)差,時(shí)間戳,求時(shí)間間隔,農(nóng)歷等等。解決辦法網(wǎng)上比比皆是,但大多...
    小李龍彪閱讀 6,758評(píng)論 1 6
  • 最近跟咱們財(cái)經(jīng)方面的專家聊天時(shí),大家都交流著最近的行情看法,都說不能很好的研究出對(duì)大家有幫助的情報(bào),其實(shí)我們都知道...
    富貴入凡塵閱讀 137評(píng)論 0 0

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