SwiftDate 浅析
2016-07-12 14:39
375 查看
SwiftDate是Github上开源的,使用Swift语言编写的NSDate封装库,可以很方便的在Swift中处理日期,比如日期创建,比较,输出等。
特性
支持数学运算符进行日期计算(比如myDate + 2.week + 1.hour)支持比较运算符(比如<,>,==,<=,>=)
快速获取/修改日期各部分内容(比如获取或修改日期中的月份)
提供通用格式化输出或自定义的格式化输出
提供一系列.toString方法
提供简便的方法获取yesterday,tomorrow等
依赖
iOS 8.0+ / Mac OS X 10.10+Xcode 6.4
Swift 1.2
支持Swift2.0版本,地址在文末的Github段落中
使用
CocoaPods安装SwiftDate
我们需要通过CocoaPods安装SwitDate,如果你还没有安装cocoapods,可以通过如下命令安装$ gem install cocoapods |
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! pod 'SwiftDate' |
$ pod install |
创建日期
通过解析字符串创建let date_custom = NSDate.date(fromString: "2015-07-26", format: DateFormat.Custom("YYYY-MM-DD")) |
let date_from_components = NSDate.date(refDate: nil, year: 2014, month: 01, day: nil, hour: nil, minute: nil, second: nil, tz: "UTC") |
let date = "2015-07-26".toDate(formatString: "YYYY-MM-DD") |
let todayDate = NSDate.today() let yesterdayDate = NSDate.yesterday() let tomorrowDate = NSDate.tomorrow() |
获取日期中年月日等信息
我们可以通过NSDate的以下属性获取.year .month .weekOfMonth .weekday .weekdayOrdinal .day .hour .minute .second .era .firstDayOfWeek // (first day of the week of passed date) .lastDayOfWeek // (last day of the week of passed date) .nearestHour // (nearest hour of the passed date) .isLeapYear() // true if date's represented year is leap .monthDays() // return the number of days in date's represented month |
修改日期
var date = NSDate() date = date.set("hour",value: 12)! date = date.set("day",value: 1)! |
日期运算
let date = NSDate() let tomorrow = date+1.day let two_months_ago = date-2.months |
时区转换
let date = NSDate() //本地时区 let date_as_utc = date.toUTC() //UTC 时间 let date_as_beijing = date_as_utc.toTimezone("UTC+8") //北京时间 |
日期比较
我们可以通过数学运算符比较let date1 = NSDate.date(fromString: "2015-07-26", format: DateFormat.Custom("YYYY-MM-DD")) let date2 = NSDate.date(fromString: "2015-07-27", format: DateFormat.Custom("YYYY-MM-DD")) if date2 > date1 { // TODO something } |
let isInRange : Bool = date1.isInTimeRange("11:00","15:00") |
.isToday() // true if represented date is today .isTomorrow() .isYesterday() .isThisWeek() // true if represented date's week is the current week .isSameWeekOf(date: NSDate) // true if two dates share the same year's week .dateAtWeekStart() // return the date where current's date week starts .beginningOfDay() // return the same date of the sender with time set to 00:00:00 .endOfDay() // return the same date of the sender with time set to 23:59:59 .beginningOfMonth() // return the date which represent the first day of the sender date's month .endOfMonth() // return the date which represent the last day of the sender date's month .beginningOfYear() // return the date which represent the first day of the sender date's year .endOfYear() // return the date which represent the last day of the sender date's year .isWeekday() // true if current sender date is a week day .isWeekend() // true if current sender date is a weekend day (sat/sun) |
NSDate转换为字符串
let string = date.toString(format: DateFormat.Custom("YYYY-MM-DD")) |
let string = date.toString(dateStyle: .ShortStyle timeStyle:.LongStyle relativeDate:true) |
.toISOString() // DateFormat.ISO8601 .toShortString() // short style, both time and date are printed .toMediumString() // medium style, both time and date are printed .toLongString() // full style, both time and date are printed .toShortDateString() // short style, print only date .toShortTimeString() // short style, print only time .toMediumDateString() // medium style, print only date .toMediumTimeString() // medium style, print only time .toLongDateString() // long style, print only date .toLongTimeString() // long style, print only time |
var d = NSDate()-2.hour var abb = d.toRelativeString(abbreviated: true, maxUnits: 3) println("data: \(abb)") |
SwiftDate 是个非常强大的日期/日历框架,完全使用 Swift 编写。SwiftDate 部分是基于 QuantLib 的,著名的定量金融学库,SwiftDate 对金融应用非常友好,同时也有强大的商业日历功能。
创建日期:
var d1 = Date(year : 2014, month : 5, day : 15) var d2 = Date(string : "2014-05-15")
简单的日期计算:
d1 = d1 + 1 if (d2 > d1) { ... }
复杂的日期转换函数:
var d = Date(string : "2014-04-30") d.addMonths(1) // returns "2014-05-30" d.addMonths(1, rollDay : RollDay.ThirtyOne) // returns "2014-05-31" d + "1M" // returns "2014-05-30"
日历
var cal = USNYSECalendar() var d1 = Date(string : "2014-07-03") var nbd = cal.nextBizDay(d1) // returns "2014-07-07" - skipped over 4th of July!
日期计算:
var dc = Actual360() dc.dayCountFraction(Date(string : "2014-01-31"), date2: Date(string : "2014-02-28"))
项目主页:
Swift日期/日历框架:SwiftDate
Github
SwiftDateSwiftDate支持Swift 2.0版本
参考链接:
1.https://www.aswifter.com/2015/07/26/use-swiftdate/
2.http://www.faceye.net/search/180350.html#bottom-ad
相关文章推荐
- swift数组与字符串互相转换
- Swift基础(八)函数和闭包
- Swift学习笔记之--类和对象
- Swift - 告警提示框(UIAlertController)的用法
- Swift学习笔记 - 教程学习四 控制流(Control Flow)
- Swift类中如何创建一个对外只读对内可读写的属性
- Swift类中如何创建一个对外只读对内可读写的属性
- Swift类中如何创建一个对外只读对内可读写的属性
- iOS10.0 & Swift 3.0 对于升级项目的建议
- yii 框架用swiftmailer 发送邮件
- iOS8自定义Collection View Cell - Swift教程
- swift学习笔记
- Swift基础(六)解包
- Swift基础(五)字典
- Swift基础(四)数组
- Swift -banner滚动图自定义
- Swift - 与OC混编时如何创建桥接头文件
- Swift - 协议(protocol)
- Swift - 文件,文件夹操作大全
- Swift - 文本标签(UILabel)的用法