前端之家收集整理的这篇文章主要介绍了
Swift设置微信朋友圈时间显示样式,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
把请求下来的时间字符串(requestDateString)转换为NSDate
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE MMM d HH:mm:ss Z yyyy"
formatter.locale = NSLocale(localeIdentifier: "en")
let requestDate = formatter.dateFromString(requestDateString)
把转换好的NSDate转换成要显示的形式
func getShowFormat(requestDate:NSDate) -> String {
let calendar = NSCalendar.currentCalendar()
if calendar.isDateInToday(requestDate) {
let since = Int(NSDate().timeIntervalSinceDate(requestDate))
if since < 60 {
return "刚刚"
}
if since < 60 * 60 {
return "\(since/60)分钟前"
}
return "\(since / (60 * 60))小时前"
}
var formatterString = "HH:mm"
if calendar.isDateInYesterday(requestDate) {
formatterString = "昨天" + formatterString
} else {
formatterString = "MM-dd" + formatterString
let comps = calendar.components(NSCalendarUnit.Year,fromDate: requestDate,toDate: NSDate(),options: NSCalendarOptions(rawValue: 0))
if comps.year >= 1 {
formatterString = "yyyy-" + formatterString
}
}
let formatter = NSDateFormatter()
formatter.dateFormat = formatterString
formatter.locale = NSLocale(localeIdentifier: "en")
return formatter.stringFromDate(requestDate)
}