歡迎加入QQ討論群258996829

Swift 日期處理庫(kù) SwiftDate

發(fā)布時(shí)間:2016-09-25 19:51  回復(fù):0  查看:8050  感興趣:55  贊:3   最后回復(fù):2016-09-25 19:51  

Swift 日期處理庫(kù) SwiftDate

SwiftDate是Github上開源的,使用Swift語言編寫的NSDate封裝庫(kù),可以很方便的在Swift中處理日期,比如日期創(chuàng)建,比較,輸出等。

特性

  • 支持?jǐn)?shù)學(xué)運(yùn)算符進(jìn)行日期計(jì)算(比如myDate + 2.week + 1.hour)
  • 支持比較運(yùn)算符(比如<,>,==,<=,>=)
  • 快速獲取/修改日期各部分內(nèi)容(比如獲取或修改日期中的月份)
  • 提供通用格式化輸出或自定義的格式化輸出
  • 提供一系列.toString方法
  • 提供簡(jiǎn)便的方法獲取yesterday,tomorrow等

創(chuàng)建日期

  • 通過解析字符串創(chuàng)建
let date_custom = NSDate.date(fromString: "2015-07-26", format: DateFormat.Custom("YYYY-MM-DD")) 
  • 通過指定日期各部分創(chuàng)建
let date_from_components = NSDate.date(refDate: nil, year: 2014, month: 01, day: nil, hour: nil, minute: nil, second: nil, tz: "UTC")
  • 通過String類的toDate方法創(chuàng)建
let date = "2015-07-26".toDate(formatString: "YYYY-MM-DD") 
  • 通過NSDate的靜態(tài)方法創(chuàng)建
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)!

日期運(yùn)算

let date = NSDate()
let tomorrow = date+1.day
let two_months_ago = date-2.months

時(shí)區(qū)轉(zhuǎn)換

let date = NSDate() //本地時(shí)區(qū)
let date_as_utc = date.toUTC() //UTC 時(shí)間
let date_as_beijing = date_as_utc.toTimezone("UTC+8") //北京時(shí)間

日期比較

我們可以通過數(shù)學(xué)運(yùn)算符比較

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

}

還可以通過NSDate的以下一些方法來比較

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轉(zhuǎn)換為字符串

let string = date.toString(format: DateFormat.Custom("YYYY-MM-DD")) 
也可以在轉(zhuǎn)換方法中指定NSDateFormatterStyle
let string = date.toString(dateStyle: .ShortStyle timeStyle:.LongStyle relativeDate:true)
還可以通過以下方法轉(zhuǎn)換為特定的字符串
.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
最后我們還可以輸出相對(duì)時(shí)間的格式,比如輸出”2 hours ago”
var d = NSDate()-2.hour
var abb = d.toRelativeString(abbreviated: true, maxUnits: 3)
println("data: \(abb)")


相關(guān)開源代碼

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?