Swift4.0之朋友圈时间格式

朋友圈时间格式
 //朋友圈时间
    func getShowFormat(requestDate:Date) -> String {
        
        //获取当前时间
        let calendar = Calendar.current
        //判断是否是今天
        if calendar.isDateInToday(requestDate as Date) {
            //获取当前时间和系统时间的差距(单位是秒)
            //强制转换为Int
            let since = Int(Date().timeIntervalSince(requestDate as Date))
            //  是否是刚刚
            if since < 60 {
                return "刚刚"
            }
            //  是否是多少分钟内
            if since < 60 * 60 {
                return "\(since/60)分钟前"
            }
            //  是否是多少小时内
            return "\(since / (60 * 60))小时前"
        }
        
        //判断是否是昨天
        var formatterString = " HH:mm"
        if calendar.isDateInYesterday(requestDate as Date) {
            formatterString = "昨天" + formatterString
        } else {
            //判断是否是一年内
            formatterString = "MM-dd" + formatterString
            //判断是否是更早期
            
            let comps = calendar.dateComponents([Calendar.Component.year], from: requestDate, to: Date())
            
            if comps.year! >= 1 {
                formatterString = "yyyy-" + formatterString
            }
        }
        
        //按照指定的格式将日期转换为字符串
        //创建formatter
        let formatter = DateFormatter()
        //设置时间格式
        formatter.dateFormat = formatterString
        //设置时间区域
        formatter.locale = NSLocale(localeIdentifier: "en") as Locale!
        
        //格式化
        return formatter.string(from: requestDate as Date)
    }


使用方法

                let requestDateString = "请求下来的"
                let formatter = DateFormatter()
               //时间字符串和以下时间格式对应
                formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
                formatter.locale = Locale.init(identifier: "en") as Locale!
                
                let requestDate = formatter.date(from: requestDateString)
                //显示时间
                timeLabel.text = getShowFormat(requestDate:requestDate!)

你可能感兴趣的:(Swift4.0之朋友圈时间格式)