最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[基本解决]swift中实现时间的人性化描述:多少天前

iOS crifan 1987浏览 0评论

需要把时间转换为:

今天,昨天,1天前,3天前,一周前,两周前,一个月前,三个月前,半年前和一年前

去参考别人已有的库:

swift nsdate lib

malcommac/SwiftDate: Powerful NSDate & Time Zones Management, made in Swift 2

kevinlawler/NSDate-TimeAgo: A "time ago", "time since", "relative date", or "fuzzy date" category for NSDate and iOS, Objective-C, Cocoa Touch, iPhone, iPad

akosma/SwiftMoment: A time and calendar manipulation library for iOS 8+, macOS 10.10+, tvOS 9+, watchOS 2+, Xcode 7 written in Swift 2.2.

devxoul/Date.swift: A Swift Date Library

最后用如下代码:

基本达到了所需要的效果:

//: Playground – noun: a place where people can play

import UIKit

extension NSDate

{

    //2015

    var Year: Int {

        let curCalendar:NSCalendar = NSCalendar.currentCalendar()

        let componentYear:Int = curCalendar.component(NSCalendarUnit.Year, fromDate: self)

        return componentYear

    }

    //11

    var Month: Int {

        let curCalendar:NSCalendar = NSCalendar.currentCalendar()

        let componentMonth:Int = curCalendar.component(NSCalendarUnit.Month, fromDate: self)

        return componentMonth

    }

    //28

    var Day: Int {

        let curCalendar:NSCalendar = NSCalendar.currentCalendar()

        let componentDay:Int = curCalendar.component(NSCalendarUnit.Day, fromDate: self)

        return componentDay

    }

    //10

    var Hour: Int {

        let curCalendar:NSCalendar = NSCalendar.currentCalendar()

        let componentHour:Int = curCalendar.component(NSCalendarUnit.Hour, fromDate: self)

        return componentHour

    }

    //39

    var Minute: Int {

        let curCalendar:NSCalendar = NSCalendar.currentCalendar()

        let componentMinute:Int = curCalendar.component(NSCalendarUnit.Minute, fromDate: self)

        return componentMinute

    }

    //18

    var Second: Int {

        let curCalendar:NSCalendar = NSCalendar.currentCalendar()

        let componentSecond:Int = curCalendar.component(NSCalendarUnit.Second, fromDate: self)

        return componentSecond

    }

    //get short style date time string

    //11/28/15, 10:51 AM

    func toStringShort() -> String {

        let dateFormatter:NSDateFormatter = NSDateFormatter()

        dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle

        dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

        let shortStyleStr:String = dateFormatter.stringFromDate(self)

        return shortStyleStr

    }

    //get medium style date time string

    //Nov 28, 2015, 10:51:33 AM

    func toStringMedium() -> String {

        let dateFormatter:NSDateFormatter = NSDateFormatter()

        dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

        dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle

        let mediumStyleStr:String = dateFormatter.stringFromDate(self)

        return mediumStyleStr

    }

    //get long style date time string

    //November 28, 2015 at 10:51:33 AM GMT+8

    func toStringLong() -> String {

        let dateFormatter:NSDateFormatter = NSDateFormatter()

        dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle

        dateFormatter.timeStyle = NSDateFormatterStyle.LongStyle

        let longStyleStr:String = dateFormatter.stringFromDate(self)

        return longStyleStr

    }

    //get full style date time string

    //Saturday, November 28, 2015 at 10:51:33 AM China Standard Time

    func toStringFull() -> String {

        let dateFormatter:NSDateFormatter = NSDateFormatter()

        dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle

        dateFormatter.timeStyle = NSDateFormatterStyle.FullStyle

        let fullStyleStr:String = dateFormatter.stringFromDate(self)

        return fullStyleStr

    }

    //get date formatted string

    //2015/11/28 10:48:12

    func toString(dateFormat:String) -> String {

        let dateFormatter:NSDateFormatter = NSDateFormatter()

        dateFormatter.dateFormat = dateFormat

        let formattedDatetimeStr:String = dateFormatter.stringFromDate(self)

        return formattedDatetimeStr

    }

    //parse input date time string into NSDate

    //input: 2015/11/28 12:01:02 and yyyy/MM/dd HH:mm:ss

    //output: Optional(2015-11-28 04:01:02 +0000)

    static func fromString(datetimeStr:String, dateFormat:String) -> NSDate? {

//        print("datetimeStr=\(datetimeStr), dateFormat=\(dateFormat)")

        let dateFormatter:NSDateFormatter = NSDateFormatter()

//        print("dateFormatter\(dateFormatter)")

        dateFormatter.dateFormat = dateFormat

//        print("dateFormatter\(dateFormatter)")

        let parsedDatetime:NSDate? = dateFormatter.dateFromString(datetimeStr)

//        print("parsedDatetime\(parsedDatetime)")

        return parsedDatetime

    }

    //from milli second timestamp to NSDate

    static func fromTimestampMsec(timestampInt64InMsec:Int64) -> NSDate {

        //        print("timestampInt64InMsec=\(timestampInt64InMsec)") //timestampInt64InMsec=1449805150184

        let timestampDoubleInSec:Double = Double(timestampInt64InMsec)/1000

        //        print("timestampDoubleInSec=\(timestampDoubleInSec)") //timestampDoubleInSec=1449805150.184

        let parsedDate:NSDate = NSDate(timeIntervalSince1970: NSTimeInterval(timestampDoubleInSec))

        //        print("parsedDate=\(parsedDate)") //parsedDate=2015-12-11 03:39:10 +0000

        return parsedDate

    }

    //static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

    //    var emptyDate:NSDate {

    //        return NSDate(timeIntervalSince1970: 0)

    //    }

    @nonobjc static let emptyDate:NSDate = NSDate(timeIntervalSince1970: 0)

    func isToday(toCmpTime:NSDate) -> Bool {

        var isToday = false

        if (toCmpTime.Year == self.Year) &&

            (toCmpTime.Month == self.Month) &&

            (toCmpTime.Day == self.Day) {

            isToday = true

        }

        return isToday

    }

    func toAgoString() -> String {

        let curDate = NSDate()

        print("self=\(self), curDate=\(curDate)")

        let components = self.toDateComponents(curDate)

        print("components.year=\(components.year), components.month=\(components.month)")

        if components.year > 0 {

//            if (components.year >= 1) {

                return "一年前"

//            }

        }

        if components.month > 0 {

            if (components.month >= 6) && (components.month < 12) {

                return "半年前"

            } else if (components.month >= 3) && (components.month < 6) {

                return "三个月前"

            } else if (components.month >= 1) && (components.month < 3) {

                return "一个月前"

            }

        }

        if components.day > 0 {

            if components.day >= 14 {

                return "两周前"

            } else if (components.day >= 7) && (components.day < 14) {

                return "一周前"

            } else if (components.day >= 3) && (components.day < 7) {

                return "三天前"

            } else if (components.day > 1) && (components.day < 3) {

                return "一天前"

            } else if components.day == 1 {

                return "昨天"

            }

        } else if components.day == 0 {

            return "今天"

        }

        return ""

    }

    func toDateComponents(toDate:NSDate) -> NSDateComponents {

        let calander = NSCalendar.currentCalendar()

        return calander.components([.Second, .Minute, .Hour, .Day, .Month, .Year], fromDate: self, toDate: toDate, options: [])

    }

}

public func ==(lhs: NSDate, rhs: NSDate) -> Bool {

    return lhs === rhs || lhs.compare(rhs) == .OrderedSame

}

public func <(lhs: NSDate, rhs: NSDate) -> Bool {

    return lhs.compare(rhs) == .OrderedAscending

}

extension NSDate: Comparable { }

//let timeToday = NSDate()

////timeToday.toHumanReadableString

//let oneYearAgo = NSDate.fromString("2015-05-12", dateFormat: "yyyy-MM-dd")!

//oneYearAgo.toAgoString()

//let twoYearAgo = NSDate.fromString("2014-06-12", dateFormat: "yyyy-MM-dd")!

////twoYearAgo.toHumanReadableString

//let sixMonthAgo = NSDate.fromString("2015-12-29", dateFormat: "yyyy-MM-dd")!

//sixMonthAgo.toAgoString()

//let threeMonthAgo = NSDate.fromString("2016-02-29", dateFormat: "yyyy-MM-dd")!

//threeMonthAgo.toAgoString()

//let oneMonthAgo = NSDate.fromString("2016-03-29", dateFormat: "yyyy-MM-dd")!

//oneMonthAgo.toAgoString()

//let oneWeekAgo = NSDate.fromString("2016-06-01", dateFormat: "yyyy-MM-dd")!

//oneWeekAgo.toAgoString()

//let threeDayAgo = NSDate.fromString("2016-06-09", dateFormat: "yyyy-MM-dd")!

//threeDayAgo.toAgoString()

//let oneDayAgo = NSDate.fromString("2016-06-12", dateFormat: "yyyy-MM-dd")!

//oneDayAgo.toAgoString()

//let yestodayAgo = NSDate.fromString("2016-06-13", dateFormat: "yyyy-MM-dd")!

//yestodayAgo.toAgoString()

//let todayAgo = NSDate.fromString("2016-06-14", dateFormat: "yyyy-MM-dd")!

//todayAgo.toAgoString()

虽然有点不是足够好:

比如跨年的时候,之前一天,其实输出:昨天

比去年,会更好些。

但是就这么着了。

懒得继续弄了。

后续整理出更通用的代码,详见:

https://github.com/crifan/crifanLib/blob/master/swift/CrifanNSDate.swift

转载请注明:在路上 » [基本解决]swift中实现时间的人性化描述:多少天前

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.167 seconds, using 22.14MB memory