调用
+ (NSString*) getStringWithTimestamp:(NSTimeInterval)timestamp formatter:(NSString*)formatter{ if ([NSString stringWithFormat:@"%@", @(timestamp)].length == 13) { timestamp /= 1000.0f; } NSDate*timestampDate=[NSDate dateWithTimeIntervalSince1970:timestamp]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:formatter]; NSString *strDate = [dateFormatter stringFromDate:timestampDate]; return strDate;} + (NSString*) getStringWithTimestamp:(NSTimeInterval)timestamp formatter:(NSString*)formatter{ if ([NSString stringWithFormat:@"%@", @(timestamp)].length == 13) { timestamp /= 1000.0f; } NSDate*timestampDate=[NSDate dateWithTimeIntervalSince1970:timestamp]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:formatter]; NSString *strDate = [dateFormatter stringFromDate:timestampDate]; return strDate;}+ (NSString*) getLiveStartTime:(NSTimeInterval)timestamp formatter:(NSString*)formatter { NSString *dataFormatter = [self getStringWithTimestamp:timestamp formatter:formatter]; NSString *timeString = @""; if ([formatter containsString:@"HH"]) { timeString = [dataFormatter componentsSeparatedByString:@" "].lastObject; } NSDate *newsDate = [NSDate dateWithTimeIntervalSince1970:timestamp/1000]; NSString *dateContent; NSTimeInterval secondsPerDay = 24 * 60 * 60; NSDate *today=[[NSDate alloc] init];// NSDate *yearsterDay=[[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay]; // NSDate *qianToday=[[NSDate alloc] initWithTimeIntervalSinceNow:-2*secondsPerDay]; NSDate *mingToday=[[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay]; //假设这是你要比较的date:NSDate *yourDate = …… //日历 NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* comp1 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:newsDate];// NSDateComponents* comp2 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:yearsterDay];// NSDateComponents* comp3 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:qianToday]; NSDateComponents* comp4 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today]; NSDateComponents* comp5 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:mingToday];// if ( comp1.year == comp2.year && comp1.month == comp2.month && comp1.day == comp2.day) {// dateContent = [NSString stringWithFormat:@"昨天 %@",timeString];// }// else// if (comp1.year == comp3.year && comp1.month == comp3.month && comp1.day == comp3.day){// dateContent = [NSString stringWithFormat:@"前天 %@",timeString];// }// else if (comp1.year == comp4.year && comp1.month == comp4.month && comp1.day == comp4.day) { dateContent = [NSString stringWithFormat:@"今天 %@",timeString]; }else if (comp1.year == comp5.year && comp1.month == comp5.month && comp1.day == comp5.day) { dateContent = [NSString stringWithFormat:@"明天 %@",timeString]; } else { //返回0说明该日期不是今天、昨天、前天 dateContent = dataFormatter; } return dateContent;}
网友评论