- var now = NSDate()
- var tomorrow = NSDate(timeIntervalSinceNow: 24*60*60)
- var yestoday = NSDate(timeInterval: -24*60*60, sinceDate: now)
- var date1 = NSDate(timeIntervalSinceReferenceDate: 3600)
- var date2 = NSDate(timeIntervalSince1970: 3600)
-
- var dateDescription = now.description
-
- var interval1 = tomorrow.timeIntervalSinceDate(now)
- var interval2 = tomorrow.timeIntervalSinceNow
- var interval3 = now.timeIntervalSinceReferenceDate
- var interval5 = now.timeIntervalSince1970
-
- date1 = NSDate.distantFuture() as NSDate
- date2 = NSDate.distantPast() as NSDate
-
- var theDayAfterTomorrow = tomorrow.dateByAddingTimeInterval(24*60*60)
-
- var isTheSameDate = theDayAfterTomorrow.isEqualToDate(NSDate(timeInterval: 2*24*60*60, sinceDate: now))
- var earlierOne = now.earlierDate(tomorrow)
- var laterOne = now.laterDate(tomorrow)
- var compareResult = now.compare(tomorrow)
2、日期转换(NSDateFormatter)
- var dateFormatter1 = NSDateFormatter()
- dateFormatter1.dateStyle = NSDateFormatterStyle.MediumStyle
- dateFormatter1.timeStyle = NSDateFormatterStyle.MediumStyle
- var now = NSDate()
- var nowString = dateFormatter1.stringFromDate(now)
- now = dateFormatter1.dateFromString(nowString)!
-
- var dateFormatter2 = NSDateFormatter()
- dateFormatter2.dateFormat = "yyyy-MM-dd HH:mm:ss"
- nowString = dateFormatter2.stringFromDate(now)
- now = dateFormatter2.dateFromString(nowString)!
3、扩展
NSDateFormatter 中提供了修改月份、星期等字符的方法,具体有兴趣的话可以参考官方文档。 关于日历(NSCalendar)、日期组件(NSDateComponents)的详细介绍后续补充。
4、结语
NSDate Class Reference
NSDateFormatter Class Reference
|
请发表评论