Swift 日期处理库 SwiftDate
Posted swift语言
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift 日期处理库 SwiftDate相关的知识,希望对你有一定的参考价值。
SwiftDate是Github上开源的,使用Swift语言编写的NSDate封装库,可以很方便的在Swift中处理日期,比如日期创建,比较,输出等。
特性
支持数学运算符进行日期计算(比如myDate + 2.week + 1.hour)
支持比较运算符(比如<,>,==,<=,>=)
快速获取/修改日期各部分内容(比如获取或修改日期中的月份)
提供通用格式化输出或自定义的格式化输出
提供一系列.toString方法
提供简便的方法获取yesterday,tomorrow等
创建日期
1 |
let date_custom = NSDate.date(fromString: "2015-07-26" , format: DateFormat.Custom( "YYYY-MM-DD" )) |
1 |
let date_from_components = NSDate.date(refDate: nil, year: 2014 , month: 01 , day: nil, hour: nil, minute: nil, second: nil, tz: "UTC" ) |
1 |
let date = "2015-07-26" .toDate(formatString: "YYYY-MM-DD" ) |
1 |
let todayDate = NSDate.today() |
2 |
let yesterdayDate = NSDate.yesterday() |
3 |
let tomorrowDate = NSDate.tomorrow() |
获取日期中年月日等信息
我们可以通过NSDate的以下属性获取
11 |
.firstDayOfWeek // (first day of the week of passed date) |
12 |
.lastDayOfWeek // (last day of the week of passed date) |
13 |
.nearestHour // (nearest hour of the passed date) |
14 |
.isLeapYear() // true if date's represented year is leap |
15 |
.monthDays() // return the number of days in date's represented month |
修改日期
2 |
date = date. set ( "hour" ,value: 12 )! |
3 |
date = date. set ( "day" ,value: 1 )! |
日期运算
2 |
let tomorrow = date+ 1 .day |
3 |
let two_months_ago = date- 2 .months |
时区转换
1 |
let date = NSDate() //本地时区 |
2 |
let date_as_utc = date.toUTC() //UTC 时间 |
3 |
let date_as_beijing = date_as_utc.toTimezone( "UTC+8" ) //北京时间 |
日期比较
我们可以通过数学运算符比较
1 |
let date1 = NSDate.date(fromString: "2015-07-26" , format: DateFormat.Custom( "YYYY-MM-DD" )) |
2 |
let date2 = NSDate.date(fromString: "2015-07-27" , format: DateFormat.Custom( "YYYY-MM-DD" )) |
还可以通过NSDate的以下一些方法来比较
01 |
let isInRange : Bool = date1.isInTimeRange( "11:00" , "15:00" ) |
03 |
.isToday() // true if represented date is today |
06 |
.isThisWeek() // true if represented date's week is the current week |
07 |
.isSameWeekOf(date: NSDate) // true if two dates share the same year's week |
08 |
.dateAtWeekStart() // return the date where current's date week starts |
09 |
.beginningOfDay() // return the same date of the sender with time set to 00:00:00 |
10 |
.endOfDay() // return the same date of the sender with time set to 23:59:59 |
11 |
.beginningOfMonth() // return the date which represent the first day of the sender date's month |
12 |
.endOfMonth() // return the date which represent the last day of the sender date's month |
13 |
.beginningOfYear() // return the date which represent the first day of the sender date's year |
14 |
.endOfYear() // return the date which represent the last day of the sender date's year |
15 |
.isWeekday() // true if current sender date is a week day |
16 |
.isWeekend() // true if current sender date is a weekend day (sat/sun) |
NSDate转换为字符串
1 |
let string = date.toString(format: DateFormat.Custom( "YYYY-MM-DD" )) |
也可以在转换方法中指定NSDateFormatterStyle
1 |
let string = date.toString(dateStyle: .ShortStyle timeStyle:.LongStyle relativeDate:true) |
还可以通过以下方法转换为特定的字符串
01 |
.toISOString() // DateFormat.ISO8601 |
02 |
.toShortString() // short style, both time and date are printed |
03 |
.toMediumString() // medium style, both time and date are printed |
04 |
.toLongString() // full style, both time and date are printed |
05 |
.toShortDateString() // short style, print only date |
06 |
.toShortTimeString() // short style, print only time |
07 |
.toMediumDateString() // medium style, print only date |
08 |
.toMediumTimeString() // medium style, print only time |
09 |
.toLongDateString() // long style, print only date |
10 |
.toLongTimeString() // long style, print only time |
最后我们还可以输出相对时间的格式,比如输出”2 hours ago”
1 |
var d = NSDate()- 2 .hour |
2 |
var abb = d.toRelativeString(abbreviated: true, maxUnits: 3 ) |
3 |
println( "data: \(abb)" ) |
开源代码主页:https://github.com/malcommac/SwiftDate
文章来自:51swift
以上是关于Swift 日期处理库 SwiftDate的主要内容,如果未能解决你的问题,请参考以下文章
使用 SwiftDate 将日期转换为多种格式
Swift 字符串转换为 SwiftDate
常用python日期日志获取内容循环的代码片段
视图或片段库为常见数据类型组成 UI
自定义 Xcode 片段库
swift常用代码片段