SwiftDate:Swift中的日期處理庫

SwiftDate是Github上開源的,使用Swift語言編寫的NSDate封裝庫,能夠很方便的在Swift中處理日期,好比日期建立,比較,輸出等。ios

特性

  • 支持數學運算符進行日期計算(好比myDate + 2.week + 1.hour)
  • 支持比較運算符(好比<,>,==,<=,>=)
  • 快速獲取/修改日期各部份內容(好比獲取或修改日期中的月份)
  • 提供通用格式化輸出或自定義的格式化輸出
  • 提供一系列.toString方法
  • 提供簡便的方法獲取yesterday,tomorrow等

依賴

  • iOS 8.0+ / Mac OS X 10.10+
  • Xcode 6.3+
  • Swift 1.2

支持Swift2.0版本,地址在文末的Github段落中git

使用

CocoaPods安裝SwiftDate

咱們須要經過CocoaPods安裝SwitDate,若是你尚未安裝cocoapods,能夠經過以下命令安裝github

$ gem install cocoapods

而後在你的Xcode項目中的Podfile文件中,添加以下內容:swift

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")
  • 經過String類的toDate方法建立
let date = "2015-07-26".toDate(formatString: "YYYY-MM-DD")
  • 經過NSDate的靜態方法建立
let todayDate = NSDate.today()
let yesterdayDate = NSDate.yesterday()
let tomorrowDate = NSDate.tomorrow()

獲取日期中年月日等信息

咱們能夠經過NSDate的如下屬性獲取spa

.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") //北京時間

日期比較

咱們能夠經過數學運算符比較rest

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的如下一些方法來比較code

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"))

也能夠在轉換方法中指定NSDateFormatterStylecomponent

let string = date.toString(dateStyle: .ShortStyle timeStyle:.LongStyle relativeDate:true)

還能夠經過如下方法轉換爲特定的字符串orm

.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

最後咱們還能夠輸出相對時間的格式,好比輸出"2 hours ago"

var d = NSDate()-2.hour
var abb = d.toRelativeString(abbreviated: true, maxUnits: 3)
println("data: \(abb)")

Github

SwiftDate
SwiftDate支持Swift 2.0版本

本文做者: 陽春麪
原文地址:http://www.aswifter.com/2015/07/26/use-swiftdate/

歡迎關注個人微信公衆號,分享Android 開發,IOS開發,Swift開發和互聯網內容
微信號:APP開發者

相關文章
相關標籤/搜索