[置顶] 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];

        }

    }


你可能感兴趣的:(ios,时间戳)