Tunits 是一个易于使用的库,用于在 iOS 中执行日期和时间的计算。Tunits 允许您快速创建给定月份的所有天数的数组或找到下一个月的第一天。
有任何评论或建议?请随时提交一个 pull request 或在 Twitter (@spitzgoby) 找到我
需要 Xcode 6 和 iOS 9+ sdk。
Tunits 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "Tunits"
要运行示例项目,请首先克隆仓库,然后在 Example 目录中运行 pod install
TimeUnit
方法可以通过创建 TimeUnit
实例来使用。默认使用 NSCalendar.currentCalendar()
,但 TimeUnit
实例公开它们的日历属性,因此可以设置自定义日历。
// Instance
let tunit = TimeUnit() // init new TimeUnit object
let daysOfCurrentMonth = tunit.daysOfMonth(NSDate()) // create array of days
// Custom Calendar
let calendar = NSCalendar.currentCalendar()
calendar.firstWeekday = 2 // set first weekday to Monday
let tunit = TimeUnit()
tunit.calendar = calendar
let daysOfCurrentWeek = tunit.daysOfWeek(NSDate())
TimeUnit
也具有其实例方法的静态版本。这使得代码更简洁,但所有计算将使用 NSCalendar.currentCalendar()
。
// Static
let daysOfCurrentMonth = TimeUnit.daysOfMonth(NSDate()) // create array of days
除了直接使用 TimeUnit
方法外,已扩展 NSDate
对象以使用返回日期的 TimeUnit
方法。这意味着可以链式调用方法以进行复杂的日期选择。
// Method chaining
let endOfNextYear = NSDate().yearAfter().endOfYear() // Last second of next year
Thomas Leu, [email protected]
Tunits 可在 MIT 许可证下获得。有关更多信息,请参阅 LICENSE 文件。