NSDate的擴充 如果只需要其中一個方法 可自行提取
extension NSDate {
/**
擷取這個月有多少天
*/
func getMonthHowManyDay() ->Int {
//我們大致可以理解為:某個時間點所在的“小單元”,在“大單元”中的數量
returnNSCalendar.currentCalendar().rangeOfUnit(.Day, inUnit:.Month, forDate:self).length
}
// 擷取日期是星期幾
func getDateWeekDay() ->Int {
let dateFmt =NSDateFormatter()
dateFmt.dateFormat ="yyyy-MM-dd HH:mm:ss"
let interval =Int(self.timeIntervalSince1970)
let days =Int(interval/86400)
let weekday = ((days +4)%7+7)%7
return weekday
}
/**
* 擷取這個月第一天是星期幾
*/
func getMontFirstWeekDay() ->Int {
//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
let calendar =NSCalendar.currentCalendar()
//這裡注意 swift要用[,]這樣方式寫
let com = calendar.components([.Year,.Month,.Day], fromDate:self)
//設定成第一天
com.day =1
let date = calendar.dateFromComponents(com)
//我們大致可以理解為:某個時間點所在的“小單元”,在“大單元”中的位置 ordinalityOfUnit
let firstWeekDay = calendar.ordinalityOfUnit(.Weekday, inUnit: .WeekOfMonth, forDate: date!)
return firstWeekDay -1
}
/**
* 擷取當前Day
*/
func getDay() ->Int {
let calendar =NSCalendar.currentCalendar()
//這裡注意 swift要用[,]這樣方式寫
let com = calendar.components([.Year,.Month,.Day], fromDate:self)
return com.day
}
/**
* 擷取當前Month
*/
func getMonth() ->Int {
let calendar =NSCalendar.currentCalendar()
//這裡注意 swift要用[,]這樣方式寫
let com = calendar.components([.Year,.Month,.Day], fromDate:self)
return com.month
}
/**
* 擷取當前Year
*/
func getYear() ->Int {
let calendar =NSCalendar.currentCalendar()
//這裡注意 swift要用[,]這樣方式寫
let com = calendar.components([.Year,.Month,.Day], fromDate:self)
return com.year
}
/**
擷取指定時間下一個月的時間
*/
func getNextDate() ->NSDate {
let calendar =NSCalendar.currentCalendar()
let com = calendar.components([.Year,.Month,.Day], fromDate:self)
com.month +=1
com.day =1
if com.month ==NSDate().getMonth() {
com.day =NSDate().getDay()
}
return calendar.dateFromComponents(com)!
}
/**
擷取指定時間上一個月的時間
*/
func getLastDate() ->NSDate {
let calendar =NSCalendar.currentCalendar()
let com = calendar.components([.Year,.Month,.Day], fromDate:self)
com.month -=1
com.day =1
if com.month ==NSDate().getMonth() {
com.day =NSDate().getDay()
}
return calendar.dateFromComponents(com)!
}
/**
擷取指定時間下一個月的長度
*/
func getNextDateLenght() ->Int {
let date =self.getNextDate()
return date.getMonthHowManyDay()
}
/**
擷取指定時間上一個月的長度
*/
func getLastDateLenght() ->Int {
let date =self.getLastDate()
return date.getMonthHowManyDay()
}
//目前時間label內容
func getTimeYYYY_MM() ->String {
let day =getDay()
let month =getMonth()
let year =getYear()
let dateString =String("\(year)年\(month)月\(day)日")
return dateString
}
/**
是否是今天
*/
func isToday()->Bool {
let calendar =NSCalendar.currentCalendar()
/// 擷取self的時間
let comSelf = calendar.components([.Year,.Month,.Day], fromDate:self)
/// 擷取當前的時間
let comNow = calendar.components([.Year,.Month,.Day], fromDate:NSDate())
return comSelf.year==comNow.year && comSelf.month==comNow.month && comSelf.day==comNow.day
}
/**
是否是這個月
*/
func isEqualMonth(date :NSDate)->Bool {
let calendar =NSCalendar.currentCalendar()
/// 擷取self的時間
let comSelf = calendar.components([.Year,.Month,.Day], fromDate:self)
/// 擷取當前的時間
let comNow = calendar.components([.Year,.Month,.Day], fromDate: date)
return comSelf.year==comNow.year && comSelf.month==comNow.month
}
/**
是否是這個月
*/
func isThisMonth()->Bool {
let calendar =NSCalendar.currentCalendar()
/// 擷取self的時間
let comSelf = calendar.components([.Year,.Month,.Day], fromDate:self)
/// 擷取當前的時間
let comNow = calendar.components([.Year,.Month,.Day], fromDate:NSDate())
return comSelf.year==comNow.year && comSelf.month==comNow.month
}
/**
分別擷取準確的年月日
*/
func getDateY_M_D(day :Int)->(year:Int,month:Int,day:Int) {
let calendar =NSCalendar.currentCalendar()
let comSelf = calendar.components([.Year,.Month,.Day], fromDate:self)
comSelf.day = day
return (comSelf.year,comSelf.month,comSelf.day)
}
/**
擷取指定date
*/
func getDate(day :Int)-> NSDate {
let calendar =NSCalendar.currentCalendar()
let comSelf = calendar.components([.Year,.Month,.Day], fromDate:self)
comSelf.day = day
return calendar.dateFromComponents(comSelf)!
}
/**
把目前時間轉化為字串
*/
func currentDateIntoString()->String {
let dateFormatter =NSDateFormatter()
dateFormatter.dateFormat ="yyyy-MM-dd HH:mm:ss"
let timeString = dateFormatter.stringFromDate(self)
return timeString
}
}