Files
libr-ios/librx/DateUtil.swift

47 lines
1.3 KiB
Swift

//
// DateUtil.swift
// librx
//
// Created by Yunlong Zheng on 15/10/6.
// Copyright © 2015年 librx. All rights reserved.
//
import Foundation
struct DateUtil {
static func format(dateString: String) -> String {
return dateString.componentsSeparatedByString("+")[0].componentsSeparatedByString(".")[0].componentsSeparatedByString("T").joinWithSeparator(" ")
}
static func timeDiff(formatterDateStr: String) -> String {
let now: NSDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let eventDate = dateFormatter.dateFromString(formatterDateStr)
if eventDate==nil {
return ""
}
let second = now.timeIntervalSinceDate(eventDate!)
if second >= 60 && second < 3600{
return String(Int(second/60)) + "分钟前"
} else if second >= 3600 && second < 84600{
return String(Int(second/3600)) + "小时前"
} else if second >= 84600 && second < 84600*30{
return String(Int(second/84600)) + "天前"
} else {
return String(Int(second/(84600*30))) + "月前"
}
}
}