码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-01-21 15:21:55      阅读:199      评论:0      收藏:0      [点我收藏+]

标签: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将标准时间转化为时间戳

标签:ios   时间戳   

原文地址:http://blog.csdn.net/darongzi1314/article/details/42967275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!