各种时间转换 ,对时间的处理

import Foundation

class TimeFormat:NSObject {

    //11:02   昨天10:20   前天12:32   3149:23

    func DayAndTime(time:String) -> String{

        let formatter = NSDateFormatter()

        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        

        if (formatter.dateFromString(time) == nil) {

            return ""

        }

        //得到分享当天的零点

        let raw_0 = (time as NSString).substringWithRange(NSMakeRange(0, 10)) + " 00:00:01"

        

        let getTime = formatter.dateFromString(raw_0)!.timeIntervalSince1970

        

        let nowTime = NSDate().timeIntervalSince1970

        

        let hourminuteTime = (time as NSString).substringWithRange(NSMakeRange(11, 5))

        

        var monthTime = (time as NSString).substringWithRange(NSMakeRange(5, 2))

        let month = (monthTime as NSString).substringWithRange(NSMakeRange(0, 1))

        if month == "0"{

            monthTime = (monthTime as NSString).substringWithRange(NSMakeRange(1, 1))

        }

        

        var dayTime = (time as NSString).substringWithRange(NSMakeRange(8, 2))

        let day = (dayTime as NSString).substringWithRange(NSMakeRange(0, 1))

        if day == "0"{

            dayTime = (dayTime as NSString).substringWithRange(NSMakeRange(1, 1))

        }

        let time5 = "\(monthTime)\(dayTime)"

        

        let offset = nowTime - getTime

        if offset > 259200 {

            return time5 + hourminuteTime

        }else if offset > 172800 {

            return "前天" + hourminuteTime

        }else if offset > 86400{

            return "昨天" + hourminuteTime

        }else{

            return hourminuteTime

        }

    }

    

    

    //今天  昨天  前天  227

    func onlyYearAndmonTh(time:String) -> String{

        let formatter = NSDateFormatter()

        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        

        if (formatter.dateFromString(time) == nil) {

            return ""

        }

        //得到分享当天的零点

        let raw_0 = (time as NSString).substringWithRange(NSMakeRange(0, 10)) + " 00:00:01"

        let getTime = formatter.dateFromString(raw_0)!.timeIntervalSince1970

        

        let nowTime = NSDate().timeIntervalSince1970

        

        var monthTime = (time as NSString).substringWithRange(NSMakeRange(5, 2))

        let month = (monthTime as NSString).substringWithRange(NSMakeRange(0, 1))

        if month == "0"{

            monthTime = (monthTime as NSString).substringWithRange(NSMakeRange(1, 1))

        }

        let dayTime = (time as NSString).substringWithRange(NSMakeRange(8, 2))

        let getNowTime = "\(monthTime)\(dayTime)"

        let offset = nowTime - getTime

        if offset > 259200 {

            return getNowTime

        }else if offset > 172800 {

            return "前天"

        }else if offset > 86400{

            return "昨天"

        }else{

            return "今天"

        }

    }

    

    

    //只有 小时和分钟

    func onlyHourAndMinute(time:String) -> String{

        

        let onlyDayTime = (time as NSString).substringWithRange(NSMakeRange(11, 5))

        

        let formatter = NSDateFormatter()

        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        

        if (formatter.dateFromString(time) == nil) {

            return ""

        }

        //得到分享当天的零点

        let raw_0 = (time as NSString).substringWithRange(NSMakeRange(0, 10)) + " 00:00:01"

        let getTime = formatter.dateFromString(raw_0)!.timeIntervalSince1970

        

        let nowTime = NSDate().timeIntervalSince1970

        

        var monthTime = (time as NSString).substringWithRange(NSMakeRange(5, 2))

        let month = (time as NSString).substringWithRange(NSMakeRange(5, 1))

        if month == "0"{

            monthTime = (time as NSString).substringWithRange(NSMakeRange(6, 1))

        }

        

        var dayTime = (time as NSString).substringWithRange(NSMakeRange(8, 2))

        let day = (time as NSString).substringWithRange(NSMakeRange(8, 1))

        if day == "0"{

            dayTime = (time as NSString).substringWithRange(NSMakeRange(9, 1))

        }

        

        let time5 = "\(monthTime)\(dayTime)"

        let offset = nowTime - getTime

        

        if offset > 259200 {

            return time5 + onlyDayTime

        }else if offset > 172800 {

            return "前天" + onlyDayTime

        }else if offset > 86400{

            return "昨天" + onlyDayTime

        }else{

            return time5

        }

        

    }

    func format_4(time:String) -> (String,String){

        let time1 = onlyYearAndmonTh(time)

        if (time1 as NSString).length > 2{

            let time2 = (time as NSString).substringWithRange(NSMakeRange(8, 1))

            var time3 = (time as NSString).substringWithRange(NSMakeRange(0, 7))

            let time3_1 = (time3 as NSString).substringWithRange(NSMakeRange(5, 1))

            if time3_1 == "0"{

                time3 = (time as NSString).substringWithRange(NSMakeRange(0, 5)) + (time as NSString).substringWithRange(NSMakeRange(6, 1))

            }

            if time2 == "0"{

                let time4 = (time as NSString).substringWithRange(NSMakeRange(9, 1)) + ""

                return (time4,time3)

            }else{

                let time4 = (time as NSString).substringWithRange(NSMakeRange(8, 2)) + ""

                return (time4,time3)

            }

        }else{

            return (time1,"")

        }

    }

    

    class func TimeLine(raw: String) -> String {

        

        //刚刚 一分钟前 45分钟前 一小时前 18小时前 1天前 50天前 100天前 1111天前 9999天前

        let formatter = NSDateFormatter()

        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        

        if (formatter.dateFromString(raw) == nil) {

            return ""

        }

        

        let getTime = formatter.dateFromString(raw)!.timeIntervalSince1970

        

        let nowTime = NSDate().timeIntervalSince1970

        

        let offset = nowTime - getTime

        

        if offset > 86400 {

            let day = Int(offset/86400)

            return "\(day)天前"

        }else if offset > 3600 {

            let hour = Int(offset/3600)

            return "\(hour)小时前"

        }else if offset > 60 {

            let minute = Int(offset/60)

            return "\(minute)分钟前"

        }else {

            return "刚刚"

        }

        

    }

    //得到日期  2016-08-25

    class func getTime(raw: String) -> String {

        let time = (raw as NSString).substringWithRange(NSMakeRange(0, 10))

        return time

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值