import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let format = "yyyy/MM/dd"
let fromString = "2020/10/21"
print("---1.格式化后的日期, Date(fromString: fromString, format: format):\(String(describing: Date(fromString: fromString, format: format)))")
let now = Date()
print("---2.日期对象转成字符串, now.toString():\(now.toString())")
print("---3.将日期对象转成指定的格式, now.toString(dateStyle: .medium, timeStyle: .medium):\(now.toString(dateStyle: .medium, timeStyle: .medium))")
print("---4. 将日期对象转成指定(字符串形式)的格式, now.toString(format:yyyy/MM/dd HH:mm:ss):\(now.toString(format: "yyyy/MM/dd HH:mm:ss"))")
let now2 = Date()
let later = Date(timeIntervalSinceNow: -100000)
print("---5.俩个日期相差的天数,later.daysInBetweenDate(now2):\(later.daysInBetweenDate(now2))")
print("---6.俩个日期相差的小时数,later.hoursInBetweenDate(now2):\(later.hoursInBetweenDate(now2))")
print("---7.俩个日期相差的分钟数,later.minutesInBetweenDate(now2):\(later.minutesInBetweenDate(now2))")
print("---8.俩个日期相差的秒数,later.secondsInBetweenDate(now2):\(later.secondsInBetweenDate(now2))")
let date1 = Date(timeIntervalSinceNow: -100000)
print("---9. 计算该日期距离当前已经走过的长度,date1.timePassed():\(date1.timePassed())")
let now3 = Date()
let now4 = Date()
print("---10.快速比较两个日期的大小, now3 == now4:\(now3 == now4)")
print("now3 < now4:\(now3 < now4)")
print("now3 < now4:\(now3 < now4)")
Timer.runThisAfterDelay(seconds: 2)
{ () -> () in
print("---11. 主线程中,延时2s执行,Prints this 2 seconds later in main queue")
}
Timer.runThisAfterDelay(seconds: 2, queue: DispatchQueue.global())
{ () -> () in
print("---12. 子线程中,延时2s执行,Prints this 2 seconds later")
}
}
}