UI:NSData_计算

时间转化

 let timeTextIndex = model.expireDatetime?.endIndex.advancedBy(-cutTimeIndex)
        let timeString = model.expireDatetime?.substringToIndex(timeTextIndex!)
        let dateFormat = NSDateFormatter()
        //2016-08-05 00:58:35.0
        dateFormat.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let expireTime = dateFormat.dateFromString(timeString!)//很明显的 正则表达式)
        let carlendar = NSCalendar.currentCalendar()
        let unitFlags = NSCalendarUnit(rawValue: NSCalendarUnit.Hour.rawValue | NSCalendarUnit.Minute.rawValue | NSCalendarUnit.Second.rawValue )
        let dateTimeNeeds = carlendar.components(unitFlags, fromDate: NSDate(), toDate: expireTime!, options: NSCalendarOptions(rawValue: 0) )
 /*
         第一个参数:需要计算年月日时分秒里面的哪一项
         第二个参数:开始时间
         第三个参数:结束时间
         第四个参数:选项
         */
        self.timeLabel?.text = String(format: "剩余 %2d: %02d: %02d .", dateTimeNeeds.hour, dateTimeNeeds.minute, dateTimeNeeds.second)

你可能感兴趣的:(UI:NSData_计算)