Swift中的时间戳工具TimeStampTools

//自己封装的简单处理时间戳的工具

class TimeStampTools: NSObject {


    static func getCurrentTimeString(format:String?) -> String {
        let timeStamp = TimeStampTools.getCurrentTimeStamp()
        return TimeStampTools.timeStampToString(timeStamp: timeStamp, format: format)
    }
    
    static func getCurrentTimeStamp() -> String {
        let date = NSDate()
        let timeInterval = date.timeIntervalSince1970
        return "\(timeInterval)";
    }
    
    static func timeStampToString(timeStamp:String,format:String?)->String {
        
        let string = NSString(string: timeStamp)
        
        let timeSta:TimeInterval = string.doubleValue
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = format == nil ? "yyyy年MM月dd日" : format
        let date = NSDate(timeIntervalSince1970: timeSta)
        return dfmatter.string(from: date as Date)
    }
    
    static func stringToTimeStamp(stringTime:String,format:String?)->String {
        
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = format == nil ? "yyyy年MM月dd日" : format
        let date = dfmatter.date(from: stringTime)
        let dateStamp:TimeInterval = date!.timeIntervalSince1970
        let dateSt:Int = Int(dateStamp)
        return String(dateSt)
    }

}

你可能感兴趣的:(Swift)