BCD7A3A49CAC5F36F8789CE4457D0D29.png
最近項(xiàng)目需要及汉,做一個(gè)錢包,里面有個(gè)類似支付寶賬單的東西削樊,界面的話就是自定義tableViewcell豁生,數(shù)據(jù)的話,頭像漫贞,金錢甸箱,名字呀,都是后臺(tái)傳的迅脐,基本不用做任何操作芍殖,直接拿來用,但是時(shí)間就不一樣了谴蔑,后臺(tái)傳的是時(shí)間戳豌骏,但是顯示的話龟梦,昨天和今天的時(shí)間周期內(nèi),上面顯示昨天或者今天的字樣窃躲,下面是時(shí)間计贰,超過這個(gè)時(shí)間范圍顯示星期和日期,這個(gè)就需要前端判斷轉(zhuǎn)為自己需要的東西了蒂窒,話不多躁倒,直接上代碼!H髯痢秧秉!
時(shí)間戳轉(zhuǎn)時(shí)間
//后臺(tái)傳過來的時(shí)間戳
NSString* jiezhiTimeString1 = [NSString stringWithFormat:@"%@",dict[@"due_time"]];
//將后臺(tái)傳給的時(shí)間戳轉(zhuǎn)為時(shí)間
NSInteger num = [jiezhiTimeString1 integerValue]/1000;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//MM-dd
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:num];
NSString *shijian = [formatter stringFromDate:confromTimesp];```
####時(shí)間戳轉(zhuǎn)星期(時(shí)間戳是當(dāng)前時(shí)間)
//獲得當(dāng)前時(shí)間的時(shí)間戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//轉(zhuǎn)為字符型
NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:dat];
NSString *weekStr = [weekday objectAtIndex:components.weekday];```
時(shí)間轉(zhuǎn)時(shí)間戳
NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精確到毫秒,不乘就是精確到秒
NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //轉(zhuǎn)為字符型 ```
####想實(shí)現(xiàn)類似支付寶的這個(gè)衰抑,就是獲得現(xiàn)在的時(shí)間戳轉(zhuǎn)為只有日分象迎,后臺(tái)傳過來的也轉(zhuǎn)為日分,兩個(gè)相減呛踊,0或者1的話顯示今天或者昨天砾淌,其他的話就顯示周幾!
####完整代碼如下(代碼里面的是model數(shù)據(jù))
//后臺(tái)傳過來的時(shí)間戳
NSString* jiezhiTimeString1 = [NSString stringWithFormat:@"%@",dict[@"due_time"]];
//將后臺(tái)傳給的時(shí)間戳轉(zhuǎn)為時(shí)間
NSInteger num = [jiezhiTimeString1 integerValue]/1000;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//MM-dd
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:num];
NSString *shijian = [formatter stringFromDate:confromTimesp];
//--------------------
//將后臺(tái)傳給的時(shí)間戳轉(zhuǎn)為日期
NSInteger num1 = [jiezhiTimeString1 integerValue]/1000;
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM-dd"];//MM-dd
NSDate *confromTimesp1 = [NSDate dateWithTimeIntervalSince1970:num1];
NSString *riqi = [formatter stringFromDate:confromTimesp1];
// 將后臺(tái)傳給的時(shí)間戳轉(zhuǎn)為星期
NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSInteger num2 = [jiezhiTimeString1 integerValue]/1000;
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:num2];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:newDate];
NSString *weekStr = [weekday objectAtIndex:components.weekday];
//--------------
//獲得當(dāng)前時(shí)間的時(shí)間戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//轉(zhuǎn)為字符型
//兩個(gè)時(shí)間戳的差轉(zhuǎn)為日
NSInteger time1 = [jiezhiTimeString1 integerValue];
NSInteger time2 = [timeString integerValue];
NSInteger response = time2 - time1;
NSTimeInterval theResponse = response;
NSDate * responseTimeInterval = [NSDate dateWithTimeIntervalSince1970:theResponse];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];//yyyy-MM-dd HH:mm:ss
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
NSString * responseTime = [dateFormatter stringFromDate:responseTimeInterval];
if ([responseTime isEqualToString:@"0"]) {
_dayStr = @"今天";
_time = shijian;
}else if ([responseTime isEqualToString:@"1"]){
_dayStr = @"昨天";
_time = shijian;
}else{
_dayStr = riqi;
_time = weekStr ;
}```