pod 'LTSwiftDate'
NSDate 转换为 String
let time = NSDate().toString(format: "MM/dd/yyyy") // 04/08/2016
let time = NSDate().toString(format: "MMM d, H:mm a") // Apr 8, 8:29 AM
String 转换为 NSDate
"04/08/2016".toDate(format: "MM/dd/yyyy")
您可以在以下位置找到格式字符串的完整参考:http://nsdateformatter.com/
LTSwiftDate 为 Int
添加了扩展以生成日期组件,它支持
秒
分钟
小时
天
周
年
例子
let now = NSDate()
let nextMinute = now + 1.minutes
let yesterday = now - 1.days
let lastMonth = now - 1.months
...
您可以使用
>
>=
==
<
<=
例子
let now = NSDate()
let tomorror = now + 1.days
if (now < tomorrow) {
...
}
now.isEqualToDate(now + 1.mins, ignoreTime=true) // true
let now = NSDate()
let startOfDay = now.startOfDay()
let endOfDay = now.endOfDay()
let now = NSDate()
let tomorrow = now + 1.days
let days = now.daysBeforeDate(tomorrow) // days will be 1