美文网首页
时间转换为小时分秒 ios

时间转换为小时分秒 ios

作者: VickyLanLan | 来源:发表于2021-12-10 18:23 被阅读0次

    调用

    + (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;}

    相关文章

      网友评论

          本文标题:时间转换为小时分秒 ios

          本文链接:https://www.haomeiwen.com/subject/npthfrtx.html