ios中的時(shí)間設(shè)置

最進(jìn)做的項(xiàng)目用到了關(guān)于時(shí)間的知識(shí)點(diǎn),下面將學(xué)習(xí)的成果總結(jié)一下,大部分有借鑒別人的。
一、將后臺(tái)返回的時(shí)間戳轉(zhuǎn)換成時(shí)間

 //時(shí)間的格式設(shè)置
 NSDateFormatter* formatter = [[NSDateFormatter alloc] init];//實(shí)例化一個(gè)NSDateFormatter對(duì)象
 [formatter setDateStyle:NSDateFormatterMediumStyle];//// 顯示"中等"的日期、時(shí)間風(fēng)格
 [formatter setTimeStyle:NSDateFormatterShortStyle];
 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//設(shè)定時(shí)間格式,這里可以設(shè)置成自己需要的格式

 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[news.addtime doubleValue]/1000];
 NSLog(@"changTime  = %@",confromTimesp);
        
 NSString *timeString=[formatter stringFromDate:confromTimesp];
 _timeLab.text=timeString;

不過一般都寫到一個(gè)方法里

//將后臺(tái)返回的時(shí)間戳轉(zhuǎn)換成自己想要的時(shí)間格式
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    //時(shí)間戳轉(zhuǎn)時(shí)間,時(shí)間的格式設(shè)置
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/1000];
//    NSLog(@"changTime  = %@",confromTimesp);
    
    NSString *time=[formatter stringFromDate:confromTimesp];
    
    return time;
}

補(bǔ)充:
參考博客NSDateFormatterStyle 幾種取值的區(qū)別

NSDateFormatterStyle幾種取值樣式  
      NSDateFormatterNoStyle  
        // 例如: (其實(shí)就是空白的,不顯示)  
      NSDateFormatterShortStyle  
        // 例如:下午7:00 | 15/5/19  
      NSDateFormatterMediumStyle  
        // 例如:下午7:00:00 | 2013年5月19日  
      NSDateFormatterLongStyle  
        // 例如:GMT +8下午7:00:00 | 2013年5月19日  
       NSDateFormatterFullStyle  
        // 例如:中國(guó)標(biāo)準(zhǔn)時(shí)間下午7:00:00 | 2013年5月19日 星期日  

二、根據(jù)今天的時(shí)間推出前幾天或者后幾天的時(shí)間

//獲取時(shí)間
調(diào)用方法
model.dateStr = [self GetDay:[NSDate date] index:i];
/*
index = 0是獲取今天的時(shí)間
index = 1是獲取明天的日期
以此往后推
*/
- (NSString *)GetDay:(NSDate *)aDate index:(NSInteger)index{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]+index)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"MM-dd"];
    return [dateday stringFromDate:beginningOfWeek];
}

三、根據(jù)時(shí)間確定星期幾

model.weekStr = [self getTheDayOfTheWeekByDateString:[NSString stringWithFormat:@"2017-%@",model.dateStr]];

//根據(jù)用戶輸入的時(shí)間(dateString)確定當(dāng)天是星期幾,輸入的時(shí)間格式 yyyy-MM-dd ,如 2015-12-18
-(NSString *)getTheDayOfTheWeekByDateString:(NSString *)dateString{
    
    NSDateFormatter *inputFormatter=[[NSDateFormatter alloc]init];
    
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *formatterDate=[inputFormatter dateFromString:dateString];
    
    NSDateFormatter *outputFormatter=[[NSDateFormatter alloc]init];
    
    [outputFormatter setDateFormat:@"EEEE-MMMM-d"];
    
    NSString *outputDateStr=[outputFormatter stringFromDate:formatterDate];
    
    NSArray *weekArray=[outputDateStr componentsSeparatedByString:@"-"];
    
    return [weekArray objectAtIndex:0];
}
最后編輯于
?著作權(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)容

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