ios 时间类转化为字符串

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
//  NSString* timeStr = [self stringWithDate: [NSDate dateWithTimeIntervalSince1970:userGifGrouptEntity.lastGiftSentTime / 1000]]

- (NSString *)stringWithDate:(NSDate *)date  {

    if (date == nil)

        return @"";

    NSDate *now = [NSDate date];

    NSTimeInterval time = [now timeIntervalSinceDate:date];

    NSCalendar* calendar = [NSCalendar currentCalendar];

    NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSHourCalendarUnit fromDate:now];

    NSDateComponents *dateComponets = [calendar components:NSYearCalendarUnit fromDate:date];

    

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setCalendar: [NSCalendar autoupdatingCurrentCalendar]];

    

    if ([nowComponents year] != [dateComponets year]) {

            [dateFormatter setDateFormat:@"MM-dd-yyyy"];

             return [dateFormatter stringFromDate:date];

    }

      int nowHour = (int)[nowComponents hour];

    int days_ago = ((int)time + 3600*(24-nowHour))/(3600*24);

    

    if (days_ago == 0){

        [dateFormatter setDateFormat:@"HH:mm"];

        return [dateFormatter stringFromDate:date];

    }else if(days_ago == 1) {

                    return @"Yesterday";

          }else{

        

            [dateFormatter setDateFormat:@"MM-dd"];

     

        return [dateFormatter stringFromDate:date];

    }

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Java DES 加密和解密源码

下一篇:通过python post提交数据的代码演示