extension NSDate {
//日期格式
enum DateFormate:String{
case YearMonthDay = "YYYYMMdd"
case MonthDay = "MMdd"
case YMD = "YYYY-MM-dd HH:mm:ss" //hh与HH的区别:分别表示12小时制,24小时制
case YMD1 = "YYYY-MM-dd"
case MD = "MM-dd"
case MDS = "MM-dd HH:mm:ss"
case ZhongWen = "yyyy年MM月dd日"
}
//获取时间的时间戳
func dateToTimeStamp() -> Int {
return Int(self.timeIntervalSince1970)
}
//将时间戳转为日期时间
class func timeStampToDate(timeStamp:Int,formate:DateFormate) -> String {
//转换为时间
let date = NSDate(timeIntervalSince1970: NSTimeInterval(timeStamp))
//格式话输出
let dformatter = NSDateFormatter()
dformatter.dateFormat = formate.rawValue
return dformatter.stringFromDate(date)
}
}
//获取时间的时间戳
NSDate().dateToTimeStamp()
//将时间戳转为日期时间
NSDate.timeStampToDate(1463637809, formate: NSDate.DateFormate.YearMonthDay)
网友评论