//把时间字符串转换成标准时间
let formatter = NSDateFormatter()
//设置时区
formatter.locale = NSLocale(localeIdentifier:"cn")
//设置日期格式
/*
E : 星期
M : 月份
d :日
H :24 小时
m: 分
s :秒
y : 年
Zone - > Z
*/
formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
if let ceartDate = created_at {
let date = formatter.dateFromString(ceartDate)
//获取当前日历
let locendar = NSCalendar.currentCalendar()
//设置要获取的比较
let unites = NSCalendarUnit(arrayLiteral: .Year,.Month,.Day,.Hour,.Minute)
//进行比较
if let dateOne = date {
let compts = locendar.components(unites, fromDate: dateOne, toDate: NSDate(), options: [])
if compts.year == 0
{
{
if compts.day == 0{
if compts.hour == 0{
if compts.minute == 0{
created_date = "刚刚"
}else {
created_date = "\(compts.minute)分钟"
}
}else {
created_date = "\(compts.hour)小时前"
}
} else if compts.day == 1 {
formatter.dateFormat = "HH:mm"
let str = formatter.stringFromDate(dateOne)
created_date = "昨天\(str)"
}
} else {
//其他月 MM -dd HH :mm
formatter.dateFormat = "MM -dd HH :mm"
let str = formatter.stringFromDate(dateOne)
created_date = "\(str)"
}
}else {
//其他月 MM -dd HH :mm
formatter.dateFormat = "MM -dd HH :mm"
let str = formatter.stringFromDate(dateOne)
created_date = "\(str)"
}
}
}
}
}
var sourcedata :String?
/// 微博来源
var source: String? {
didSet {
// 判断不能为空
guard let _ = source else {
return
}
let soureStr = source?.componentsSeparatedByString(">")
if soureStr?.count > 1{
let str = soureStr?[1]
let strTwo = str?.componentsSeparatedByString("<")
sourcedata = strTwo?.first
}
}