时间戳

时间字符串从网上请求下来的字符串NSString * expireDatetime = [dictionary objectForKey:@"expireDatetime"];

因为剩余时间有可能为零

 //剩余时间可能出现null的格式 运行的时候会出错 所以要把这样的信息过滤

    if([expireDatetime isKindOfClass:[NSNull class]])

    {

        self.timeLabel.text = @"剩余:00:00:00";

    }

    else

    {

        //定义时间戳样式

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

        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.0";

        //结束时间

        NSDate * endDate = [formatter dateFromString:expireDatetime];

        //获取剩余时间

        NSTimeInterval interval = [endDate timeIntervalSinceNow];

        //获取时分秒

        int HH = (int)interval / 3600;

        int MM = (int)interval / 60 % 60;

        int SS = (int)interval % 60;

        

        if(HH < 0 || MM < 0 || SS < 0)

        {

            self.timeLabel.text = @"剩余:00:00:00";

        }

        else

        {

            self.timeLabel.text = [NSString stringWithFormat:@"剩余:%.2d:%.2d:%.2d",HH,MM,SS];

        }

    }

    

 //设置日期的最小值

        NSDate * date = [formatter dateFromString:@"1970-01-01"];

        _datePicker.minimumDate = date;

 

 

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