最近项目需要,做一个钱包,里面有个类似支付宝账单的东西,界面的话就是自定义tableViewcell,数据的话,头像,金钱,名字呀,都是后台传的,基本不用做任何操作,直接拿来用,但是时间就不一样了,后台传的是时间戳,但是显示的话,昨天和今天的时间周期内,上面显示昨天或者今天的字样,下面是时间,超过这个时间范围显示星期和日期,这个就需要前端判断转为自己需要的东西了,话不多,直接上代码!!!
时间戳转时间
//后台传过来的时间戳
NSString* jiezhiTimeString1 = [NSString stringWithFormat:@"%@",dict[@"due_time"]];
//将后台传给的时间戳转为时间
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];```
####时间戳转星期(时间戳是当前时间)
//获得当前时间的时间戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
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];```
时间转时间戳
NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型 ```
####想实现类似支付宝的这个,就是获得现在的时间戳转为只有日分,后台传过来的也转为日分,两个相减,0或者1的话显示今天或者昨天,其他的话就显示周几!
####完整代码如下(代码里面的是model数据)
//后台传过来的时间戳
NSString* jiezhiTimeString1 = [NSString stringWithFormat:@"%@",dict[@"due_time"]];
//将后台传给的时间戳转为时间
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];
//--------------------
//将后台传给的时间戳转为日期
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];
// 将后台传给的时间戳转为星期
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];
//--------------
//获得当前时间的时间戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
//两个时间戳的差转为日
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 ;
}```