iOS将标准时间转化为时间戳

NSDateFormatter *date=[[NSDateFormatteralloc] init];

    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

   NSDate *d=[date dateFromString:theDate];

    NSTimeInterval late=[dtimeIntervalSince1970]*1;

    NSDate* dat = [NSDatedateWithTimeIntervalSinceNow:0];

    NSTimeInterval now=[dattimeIntervalSince1970]*1;

   NSString *timeString=@"";

   NSTimeInterval cha=now-late;

   if (cha/3600<1) {

        timeString = [NSStringstringWithFormat:@"%f", cha/60];

        timeString = [timeStringsubstringToIndex:timeString.length-7];

       int num= [timeString intValue];

       if (num <= 1) {

            timeString = [NSStringstringWithFormat:@"刚刚..."];

        }else{

            timeString = [NSStringstringWithFormat:@"%@分钟前", timeString];

        }

    }

   if (cha/3600>1&&cha/86400<1)//86400=1

    {

        timeString = [NSStringstringWithFormat:@"%f", cha/3600];

        timeString = [timeStringsubstringToIndex:timeString.length-7];

        timeString=[NSStringstringWithFormat:@"%@小时前", timeString];

    }

   if (cha/86400>1)

    {

        timeString = [NSStringstringWithFormat:@"%f", cha/86400];

        timeString = [timeStringsubstringToIndex:timeString.length-7];

       int num=[timeString intValue];

       if (num<2) {

            timeString=[NSStringstringWithFormat:@"昨天"];

        }elseif (num==2){

            timeString = [NSStringstringWithFormat:@"前天"];

        }elseif (num>2&&num<7){

            timeString = [NSStringstringWithFormat:@"%@天前", timeString];

        }elseif (num >= 7 && num <=30) {

            timeString = [NSStringstringWithFormat:@"1周前"];

        }elseif(num > 30 && num <=365){

           int month=cha/(86400*30);

            timeString=[NSStringstringWithFormat:@"%d",month];

            timeString = [NSStringstringWithFormat:@"%@周前",timeString];

        }elseif (num >365){

           int year=cha/(86400*365);

            timeString=[NSStringstringWithFormat:@"%d",year];

            timeString = [NSStringstringWithFormat:@"%@年前",timeString];

        }

    }


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。