mirror of
https://github.com/wahyd4/libr-ios.git
synced 2026-08-09 05:06:46 +10:00
47 lines
1.3 KiB
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))) + "月前"
|
|
}
|
|
|
|
}
|
|
} |