• Swift 3.0 Date的简单使用


    //
    //  ViewController.swift
    //  Date的使用
    //
    //  Created by 思 彭 on 16/9/20.
    //  Copyright © 2016年 思 彭. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        
            /// 1.获得当前的日期和时间: currnetDate = 2016-09-20 02:22:22 +0000
            let currentDate = Date()
            print("currnetDate = (currentDate)")
            
            // 2.初始化DateFormatter类
            let dateFormatter = DateFormatter()
            dateFormatter.locale = Locale.current()
            
            // 3.date转string :带样式输出日期:  dateString =
            dateFormatter.dateStyle = DateFormatter.Style.noStyle
            let dateString = dateFormatter.string(from: currentDate)
            print("dateString = (dateString)")
            
            // date1String = 9/20/16
            dateFormatter.dateStyle = DateFormatter.Style.shortStyle
            let date1String = dateFormatter.string(from: currentDate)
            print("date1String = (date1String)")
            
            // date2String = Tuesday, September 20, 2016
            dateFormatter.dateStyle = DateFormatter.Style.fullStyle
            let date2String = dateFormatter.string(from: currentDate)
            print("date2String = (date2String)")
    
            // date3String = September 20, 2016
            dateFormatter.dateStyle = DateFormatter.Style.longStyle
            let date3String = dateFormatter.string(from: currentDate)
            print("date3String = (date3String)")
    
            // date4String = Sep 20, 2016
            dateFormatter.dateStyle = DateFormatter.Style.mediumStyle
            let date4String = dateFormatter.string(from: currentDate)
            print("date4String = (date4String)")
            
            // 4.自定义输出格式  date5Str = Tuesday, September, 20, 2016
            dateFormatter.dateFormat = "EEEE, MMMM, dd, yyyy"
            let date5Str = dateFormatter.string(from: currentDate)
            print("date5Str = (date5Str)")
            
            // 5. string转date  date = Optional(2016-12-02 10:15:59 +0000)
    
            let str = "2016-12-02 18:15:59"
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let date = dateFormatter.date(from: str)
            print("date = (date!)")// 加上!  输出: date = 2016-12-02 10:15:59 +0000
            
            // 6.DateCopmonents的使用
            // era:1 year:2016 month:9 day: 20 hour:10 minute:53 second:  0
            let currentCalendar = Calendar.current()
            let dateComponents = currentCalendar.components([Calendar.Unit.era,Calendar.Unit.year,Calendar.Unit.month,Calendar.Unit.day,Calendar.Unit.hour,Calendar.Unit.second,Calendar.Unit.minute], from: currentDate)
            print("era:(dateComponents.era!) year:(dateComponents.year!) month:(dateComponents.month!) day: (dateComponents.day!) hour:(dateComponents.hour!) minute:(dateComponents.minute!) second:  (dateComponents.second!)")
            
            // 7.DateComponents -> Date
            // compents = 2016-09-19 16:00:00 +0000
            var componrnts = DateComponents()
            componrnts.year = 2016
            componrnts.month = 09
            componrnts.day = 20
            let compents = currentCalendar.date(from: componrnts)
            print("compents = (compents!)");
            
            // 8.比较日期和时间
            dateFormatter.dateFormat = "MMM dd, yyyy zzz"
            let str1 = "May 08, 2016 GMT"
            let date1 = dateFormatter.date(from: str1)
            let str2 = "May 10, 2016 GMT"
            let date2 = dateFormatter.date(from: str2)
            
            // date1:Optional(2016-05-08 00:00:00 +0000)----date2:Optional(2016-05-10 00:00:00 +0000)
            print("date1:(date1)----date2:(date2)")
            
            // 2016-05-08 00:00:00 +0000    2016-05-10 00:00:00 +0000
            print((date1! as NSDate).earlierDate(date2!));  //日期更早的NSDate对象
            print((date1! as NSDate).laterDate(date2!));  //日期更晚的NSDate对象
    
            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")
            }
            
            // 9. 日期的计算
            let monthsToAdd = 2
            let daysToAdd = 5
            
            var calculatedDate = currentCalendar.date(byAdding: Calendar.Unit.month, value: monthsToAdd, to:   currentDate, options: Calendar.Options.init(rawValue: 0))
            calculatedDate = currentCalendar.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)
        }
    
     
       
        
    
    }
  • 相关阅读:
    Wampserver主机服务配置方法
    工作记录
    Shell教程
    简单教程
    java笔记
    四轴飞行器
    ace文档
    马尔科夫模型参考
    电子书搜索
    免费电子书
  • 原文地址:https://www.cnblogs.com/pengsi/p/5888023.html
Copyright © 2020-2023  润新知