iOS 日期處理 (Swift3.0 NSDate)

處理日期的常見情景

  1. NSDate -> String & String -> NSDatehtml

  2. 日期比較ios

  3. 日期計算(基於參考日期 +/- 必定時間)swift

  4. 計算日期間的差別數組

  5. 拆解NSDate對象(分解成year/month/day/hour/minute/second 等)app

NSDate相關類

  1. NSDate學習

  2. DateFormatter編碼

  3. DateComponentscode

  4. DateComponentFormattercomponent

  5. Calendarorm

  6. Date structure: Swift3.0中引入了Date structure, 和NSDate提供的功能類似, 而且Date結構體和NSDate類能夠在Swift中交替使用以此達到和Objective-C APIs的交互. Date結構體的引入和Swift中引入了String, Array等類型同樣, 都是爲了讓對應的類橋接Foundation中對應的類(String -> NSString, Array -> NSArray)

注: 在下面的代碼片斷中, NSDate和Date會交替使用,功能都是相同的.

基本概念

  • 在具體開始寫代碼以前, 搞清楚一些基本的概念是十分必要的:

    • NSDate對象: 同時能夠描述日期和時間, 當要處理日期或者時間時會使用到.

    • DateFormatter對象: 格式對象只要在將NSDate和String相互轉換的時候纔有價值, 它是用來規定格式的. 包括系統自帶的格式和手動自定義的格式,同時該類也支持時區的設置.

    • DateComponents類: 能夠看作是NSDate的姊妹類. 由於它提供了不少實用的特性和操做. 最重要的一個特性就是它能夠把日期或者時間拆解開來, 即日期或者時間裏的每個部分(好比年,月,日,小時,分鐘等)均可以單獨取出來,而且進行其餘的操做(好比計算).

    • DateComponentsFormatter類: 用來將計算機讀入的日期和時間輸出爲了人類可讀的字符串.

    • Calendar類: 日期類能夠實現NSDate和DateComponents之間的轉換.

NSDate和String之間的轉換

  • 得到當前的日期和時間

    let currentDate = Date() 
    print(currentDate) // 2016-08-19 05:33:48 +0000 格林威治時間
    

      

  • 初始化DateFormatter類

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale.current() // 設置時區
    

      

  • 使用系統自帶樣式輸出日期

    • 在將NSDate對象轉換成String類型前, 首先須要告訴計算機你想要輸出什麼樣的日期格式. 這裏有兩種方式. 第一就是使用系統自帶的格式, 第二個方法是手動使用特定的說明符來指定輸出格式.這裏先看系統自帶格式的輸出.

      dateFormatter.dateStyle = DateFormatter.Style.noStyle
      var stringDate = dateFormatter.string(from: currentDate)
      print(stringDate) // "無輸出"
       
      dateFormatter.dateStyle = DateFormatter.Style.shortStyle
      stringDate = dateFormatter.string(from: currentDate)
      print(stringDate) // 8/19/16
       
      dateFormatter.dateStyle = DateFormatter.Style.mediumStyle
      stringDate = dateFormatter.string(from: currentDate)
      print(stringDate) // Aug 19, 2016
       
      dateFormatter.dateStyle = DateFormatter.Style.longStyle
      stringDate = dateFormatter.string(from: currentDate)
      print(stringDate) // August 19, 2016
       
      dateFormatter.dateStyle = DateFormatter.Style.fullStyle
      stringDate = dateFormatter.string(from: currentDate)
      print(stringDate) // Friday, August 19, 2016
      

        

  • 使用自定義說明符輸出日期

    • EEEE: 表明一天的全名,好比Monday.使用1-3個E就表明簡寫,好比Mon.

    • MMMM: 表明一個月的全名,好比July.使用1-3個M就表明簡寫,好比Jul.

    • dd: 表明一個月裏的幾號,好比07或者30.

    • yyyy: 表明4個數字表示的年份,好比2016.

    • HH: 表明2個數字表示的小時,好比08或17.

    • mm: 表明2個數字表示的分鐘,好比01或59.

    • ss: 表明2個數字表示的秒,好比2016.

    • zzz: 表明3個字母表示的時區,好比GTM(格林尼治標準時間,GMT+8爲北京所在的時區,俗稱東八區)

    • GGG: BC或者AD, 即公元前或者公元

    • 系統自帶的樣式不夠用時, 就可使用自定義說明符自定義Date的輸出格式.

    • 自定義說明符的另外一個巨大的做用就是能夠將複雜的字符類型的日期格式(好比Fri, 08 Aug 2016 09:22:33 GMT)轉換成Date類型.

    • 自定義格式的使用最重要的就是自定義說明符的使用,說明符是一些對日期對象有特色含義的簡單的字符.下面首先列舉一些這裏會用到的說明符:

    • 關於說明符的具體介紹,請參照官方文檔

    • 繼續來看自定義說明符的實際使用, 下面將如今的日期轉換成字符串類型, 而且輸出星期和月份的全名, 年份和天數用數字表示:

      dateFormatter.dateFormat = "EEEE, MMMM, dd, yyyy"
      stringDate = dateFormatter.string(from: currentDate)
      print(stringDate) // Friday, August, 19, 2016
      

        

    • 從例子中能夠很直觀的看到其實自定義輸出格式的技術很簡單, 具體的輸出格式根據具體的需求而定, 最後再舉一個例子(輸出格式--> 小時:分鐘:秒 星期簡寫 月份顯示數字 天數顯示數字 時區 公元):

      dateFormatter.dateFormat = "HH:mm:ss E M dd zzz GGG"
      stringDate = dateFormatter.string(from: currentDate)
      print(stringDate) // 14:20:31 Fri 8 19 GMT+8 AD
      

        

  • 上面例子所有是Date轉String, 這個轉換過程的逆轉換更加有趣. 以前用到的系統自帶的輸出格式和自定義的說明符在String轉Date的過程當中一樣適用. String轉Date過程當中最重要的一點就是要設置合適的格式對應與String, 不然輸出會是nil.

    var dateString = "2016-12-02 18:15:59"
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    print(dateFormatter.date(from: dateString)) // 2016-12-02 10:15:59 +0000
    

      

    • 這裏須要注意的是字符串的時間是18:15:59, 而輸出的時間是10:15:59, 緣由是由於我所在的時區是北京所在的時區,即東八區,而默認輸出是按格林尼治標準時間輸出的,即相差8小時.

    • 下面舉一個更復雜的例子, 包括時區的輸出:

      dateString = "Mon, 08, Aug 2008 20:00:01 GMT"
      dateFormatter.dateFormat = "E, dd, MM yyyy HH:mm:ss zzz"
      dateFromString = dateFormatter.date(from: dateString)
      print(dateFromString) // 2008-08-08 20:00:01 +0000
      

        

DateComponents

  • 在不少情景中,你可能須要將日期的值進行拆解,進而提取其中特定的值.好比你可能須要獲得一個日期中天數和月份的值,或者從時間裏面獲得小時和分鐘的值,這部分將會介紹DateComponents類以此來解決這個問題. 須要注意的是,DateComponents類會常常和Calendar搭配使用,具體來說,Calendar類的方法是真正實現了Date到DateComponents的轉換,以及逆轉換.記住這一點以後,首先先獲得currentCalendar對象,而且將它賦值給常量以便於後面的使用.

    let calendar = Calendar.current()
    

      

  • 下面的代碼經過一個典型的例子演示一遍Date -> DateComponents的過程.

    let dateComponents = calendar.components([Calendar.Unit.era, Calendar.Unit.year,    Calendar.Unit.month, Calendar.Unit.day, Calendar.Unit.hour, Calendar.Unit.minute,   Calendar.Unit.second], from: currentDate)
    print("era:(dateComponents.era) year:(dateComponents.year) month:(dateComponents.month) day: (dateComponents.day) hour:(dateComponents.hour) minute:(dateComponents.minute) second:  (dateComponents.second)")
    // era:Optional(1) year:Optional(2016) month:Optional(8) day:Optional(19) hour:Optional(15)     minute:Optional(29) second:Optional(13)
    

      

    • 上面用到了Calendar類中的components(_:from:)方法. 這個方法接收兩個參數, 第二個傳入的參數是將要被拆解的日期對象,第一個參數比較有意思, 這個參數是一個數組,裏面放入組成日期的各個成分單位(Calendar.Unit),好比月(Calendar.Unit.month), 日(Calendar.Unit.day).

    • Calendar.Unit是一個結構體, 它裏面的全部屬性及說明能夠在官方文檔中查看.

    • 還須要注意的一點就是在components(_:from:)方法的第一個數組參數中,若是沒有傳入想要解析的單位名稱,以後從DateComponents對象中是得不到這個單位的, 好比上面的方法中沒有傳入Calendar.Unit.month, 那麼最後打印的時候若是也打印了該值, 獲得的值會是nil.

      dateComponents = calendar.components([Calendar.Unit.year], from: currentDate)
      print("year:(dateComponents.year) month:(dateComponents.month)")
      // Optional(2016) month:nil
      

        

  • DateComponents -> Date

    • DateComponents -> Date的轉換也十分簡單, 只須要初始化一個DateComponents對象, 而後指定特定的components, 最後調用Calendar類的dateFromComponents:方法完成轉換

      var components = DateComponents()
      components.year = 1985
      components.month = 02
      components.day = 05
      components.hour = 07
      components.minute = 08
      components.second = 44
      let dateFromComponents = calendar.date(from: components)
      print(dateFromComponents) // Optional(1985-02-04 23:08:44 +0000)
      

        

  • 這裏一樣能夠設置不一樣的時區來獲得當地的時間:

    components.timeZone = TimeZone(abbreviation: "GMT") // Greenwich Mean Time
    components.timeZone = TimeZone(abbreviation: "CST") // China Standard Time
    components.timeZone = TimeZone(abbreviation: "CET") // Central European Time
    

      

比較日期和時間

  • 除了以上提到的應用場景, 另外一個關於日期和時間常見的應用場景就是比較. 經過對兩個Date對象的比較以此來判斷哪一個日期更早或者更遲,或者是否日期相同. 這裏將會介紹3種方法來作比較, 方法不分好壞, 適合本身的需求最重要.

  • 在開始進行比較以前, 先建立兩個Date對象用於下面的比較:

    dateFormatter.dateFormat = "MMM dd, yyyy zzz"
    dateString = "May 08, 2016 GMT"
    var date1 = dateFormatter.date(from: dateString)
     
    dateString = "May 10, 2016 GMT"
    var date2 = dateFormatter.date(from: dateString)
     
    print("date1:(date1)----date2:(date2)")
    // date1:Optional(2016-05-08 00:00:00 +0000)
    // date2:Optional(2016-05-10 00:00:00 +0000)
    

      

方法1 (earlierDate: || laterDate:)

  • 當比較date1和date2兩個日期哪一個更早或更晚時, 使用NSDate類提供的earlierDate:laterDate:方法就能夠實現.

    print((date1! as NSDate).earlierDate(date2!)) // 2016-05-08 00:00:00 +0000
    print((date1! as NSDate).laterDate(date2!)) // 2016-05-10 00:00:00 +0000
    

      

    • 當使用earlierDate:方法時, 返回值是日期更早的NSDate對象; laterDate:的返回值是日期更晚的NSDate對象.

    • 上面的方法中, 由於date1和date2屬於Date類,而earlierDate:laterDate:方法想要接收的參數類型是NSDate, 因此先進行了類型轉換.

方法2 (compare: )

  • 第二個比較的方法是使用Date結構體提供的compare:方法, 返回值是ComparisonResult類型的枚舉.

    if date1?.compare(date2!) == ComparisonResult.orderedAscending {
        print("date1 is earlier") 
    } else if date1?.compare(date2!) == ComparisonResult.orderedDescending {
        print("date2 is earlier")
    } else if date1?.compare(date2!) == ComparisonResult.orderedSame {
        print("Same date!!!")
    }
    

      

方法3 (timeIntervalSinceReferenceDate)

  • 第三個方法有點不一樣, 原理是分別將date1 和 date2 與一個參考日期進行比對, 而後經過判斷兩個日期和參考日期的差距, 進而判斷兩個日期的差距.

  • 舉一個例子: 比較4和10兩個數字, 先選取6做爲一個參考數字, 4-6=-2;10-6=4,4-(-2)=6.

    if date1?.timeIntervalSinceReferenceDate > date2?.timeIntervalSinceReferenceDate {
        print("date1 is later")
    } else if date1?.timeIntervalSinceReferenceDate < date2?.timeIntervalSinceReferenceDate {
        print("date2 is later")
    } else if date1?.timeIntervalSinceReferenceDate == date2?.timeIntervalSinceReferenceDate {
        print("Same Date")
    }
    

      

日期的計算

  • 處理日期的另外一個有趣的場景就是日期的計算. 好比計算2016-08-12號加2個月零12天是多少諸如此類的計算. 這裏將介紹兩種不一樣的方法來進行日期計算. 第一個方法使用了Calendar類的CalendarUnit結構體; 第二個方法使用到了DateComponents類.

  • 首先記住當前的日期:

    print(NSDate()) // 2016-08-19 08:29:12 +0000
    

      

  • 下面假設想計算當前日期再加2個月零5天

    let monthsToAdd = 2
    let daysToAdd = 5
    

      

方法1

var calculatedDate = calendar.date(byAdding: Calendar.Unit.month, value: monthsToAdd, to:   currentDate, options: Calendar.Options.init(rawValue: 0))
calculatedDate = calendar.date(byAdding: Calendar.Unit.day, value: daysToAdd, to:   calculatedDate!, options: Calendar.Options.init(rawValue: 0))
print(calculatedDate) // Optional(2016-10-24 08:33:41 +0000)

  

  • 如上面代碼所示, 這裏使用了dateByAddingUnit:value:toDate:options:方法. 這個方法就是將特定的日期單位值加到現有的日期值上, 而後返回一個新的Date對象. 由於這裏的例子要加兩個值, 因此須要調用該方法兩次.

方法2

  • 方法1雖然可行, 但有缺點, 即每個單位的計算都須要分別調用方法, 假如想給當前日期加上7年3個月4天8小時23分鐘34秒, 那麼就要調用dateByAddingUnit:value:toDate:options:方法6次! 而方法2則能夠對這個問題迎刃而解. 而方法2有一次使用到了DateComponents這個類.

  • 下面我將首先初始化一個新的DateComponents對象, 而後設置月份和天數, 而後再調用Calendar類中名爲dateByAddingComponents:toDate:options:的方法, 該方法的返回值就是計算好的Date對象.

    var newDateComponents = DateComponents()
    newDateComponents.month = 2
    newDateComponents.day = 5
    calculatedDate = calendar.date(byAdding: newDateComponents, to: currentDate, options:   Calendar.Options.init(rawValue: 0))
    print(calculatedDate) // Optional(2016-10-24 08:47:57 +0000)
    

      

日期差值的計算

  • 計算兩個日期間具體的差值也是在處理日期對象時常常會遇到的問題. 這裏將介紹3中方法來解決該問題.

  • 首先, 先自定義兩個日期對象:

    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    dateString = "2016-08-08 18:25:17"
    date1 = dateFormatter.date(from: dateString)
     
    dateString = "1985-02-05 07:45:38"
    date2 = dateFormatter.date(from: dateString)
    

      

方法1

  • 方法1用到的是Calendar類中的一個對象方法components:from:to:options:.

    var diffComponents = calendar.components([Calendar.Unit.year, Calendar.Unit.month,  Calendar.Unit.day], from: date2!, to: date1!, options: Calendar.Options.init(rawValue: 0))
     
    print("date1 and date2 are different in (diffComponents.year)years    (diffComponents.month)months and (diffComponents.year)days")
    // date1 and date2 are different in Optional(31)years Optional(6)months and Optional(31)days
    

      

    • components:from:to:options:方法中的第一個參數仍是接收一個包含了Calendar.Unit結構體的數組. 由於是從date2比date1,升序的返回值就是正數,若是是反過來比,返回值就是負數.

方法2

  • 這裏介紹的第二個方法將會第一次使用到DateComponentsFormatter類. DateComponentsFormatter類提供了不少不一樣的方法進行日期間自動的計算,而且返回值是格式化的字符串.

  • 首先初始化一個DateComponentsFormatter對象, 而後先指定它的一個屬性值.

    let dateComponentsFormatter = DateComponentsFormatter()
    dateComponentsFormatter.unitsStyle = DateComponentsFormatter.UnitsStyle.full
    

      

    let interval = date2?.timeIntervalSince(date1!)
    var diffString = dateComponentsFormatter.string(from: interval!)
    print(diffString) // Optional("-31 years, 6 months, 0 weeks, 3 days, 10 hours, 39 minutes, 39 seconds")
    

      

    • UnitsStyle屬性會指定最後結果輸出的格式. 這裏使用full,就表明結果輸出的單位會全名顯示,如Monday,June等.若是想用簡寫,就能夠從新設置屬性.官方文檔中列出了全部的屬性值。

方法3

dateComponentsFormatter.allowedUnits = [Calendar.Unit.year]
diffString = dateComponentsFormatter.string(from: date1!, to: date2!)
print(diffString) // Optional("-31 years")

  

  • 最後一個方法調用了stringFrom:to:方法來計算. 注意使用該方法以前, 必須至少在allowedUnits屬性中設置一個calendar unit, 不然這個方法將會返回nil, 因此在使用該方法以前, 先指定想要怎麼樣看到輸出, 而後再讓執行輸出的方法。

總結

就像我在摘要中說的, 處理日期很常見, 在平常代碼中也不可避免, 這裏我經過一些小的代碼片斷介紹了處理日期的一些常見方法. 不論是NSDate類,Date結構體仍是與之相關聯的類, 它們的目的只有一個, 就是可以快速的處理日期. 若是想深刻掌握有關日期的處理, 仍是要在平常編碼過程當中多多練習, 多多閱讀官方文檔

 

學習收藏, 轉自:

Swift3.0中關於日期類的使用指引

 

其餘開發過程當中遇到的問題,後續將進行補充。。。

相關文章
相關標籤/搜索