swift 从swift中的日期获取日历信息。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 从swift中的日期获取日历信息。相关的知识,希望对你有一定的参考价值。

let currentDate = NSDate()
var dateAsString = "12-24-2015 23:59"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
var newdate = dateFormatter.dateFromString(dateAsString)



let calendar = NSCalendar.currentCalendar()

let dateComponents = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.WeekOfYear, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second, NSCalendarUnit.Nanosecond], fromDate: newdate!)

print("day = \(dateComponents.day)", "month = \(dateComponents.month)", "year = \(dateComponents.year)", "week of year = \(dateComponents.weekOfYear)", "hour = \(dateComponents.hour)", "minute = \(dateComponents.minute)", "second = \(dateComponents.second)", "nanosecond = \(dateComponents.nanosecond)" , separator: ", ", terminator: "")

//"day = 24, month = 12, year = 2015, week of year = 52, hour = 23, minute = 59, second = 0, nanosecond = 0"

以上是关于swift 从swift中的日期获取日历信息。的主要内容,如果未能解决你的问题,请参考以下文章

如何在swift3中获取两个日期之间的日历日[重复]

swift 3或objective c中的水平日历视图

Swift 4.0 Eventkit 无法正确获取日历和事件

swift中的日期格式错误

swift 在Swift中使用日期,日历。

Swift FSCalendar 在表格视图的日历显示事件中选择日期