SwiftDate
是Github上开源的,使用Swift语言编写的NSDate封装库,能够很方便的在Swift中处理日期,好比日期建立,比较,输出等。ios
支持Swift2.0版本,地址在文末的Github段落中git
咱们须要经过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")
let date = "2015-07-26".toDate(formatString: "YYYY-MM-DD")
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)
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)")
SwiftDate
SwiftDate支持Swift 2.0版本
本文做者: 阳春面
原文地址:http://www.aswifter.com/2015/07/26/use-swiftdate/
欢迎关注个人微信公众号,分享Android 开发,IOS开发,Swift开发和互联网内容
微信号:APP开发者