nsdate Extension If only one of the methods is needed to extract the
Extension NSDate {
/**
Get how many days of this month
*/
Func getmonthhowmanyday ()->int {
We can generally understand that the "small unit" at a point in time, the number in the "Big unit"
Returnnscalendar.currentcalendar (). Rangeofunit (. Day, Inunit:. Month, fordate:self). length
}
Get date is day of the week
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
}
/**
* Get the first day of the month is the week
*/
Func getmontfirstweekday ()->int {
1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
Let Calendar =nscalendar.currentcalendar ()
Notice here that Swift will write in [,] this way
Let com = calendar.components ([. Year,. Month,. Day], fromdate:self
Set into the first day
Com.day =1
Let date = calendar.datefromcomponents (COM)
We can generally understand that the "small unit" at a point in time, the position in the "Big unit" Ordinalityofunit
Let Firstweekday = Calendar.ordinalityofunit (. Weekday, Inunit:. Weekofmonth, fordate:date!)
Return firstWeekDay-1
}
/**
* Get current day
*/
Func getday ()->int {
Let Calendar =nscalendar.currentcalendar ()
Notice here that Swift will write in [,] this way
Let com = calendar.components ([. Year,. Month,. Day], fromdate:self
Return Com.day
}
/**
* Get Current month
*/
Func getmonth ()->int {
Let Calendar =nscalendar.currentcalendar ()
Notice here that Swift will write in [,] this way
Let com = calendar.components ([. Year,. Month,. Day], fromdate:self
Return Com.month
}
/**
* Get Current year
*/
Func getyear ()->int {
Let Calendar =nscalendar.currentcalendar ()
Notice here that Swift will write in [,] this way
Let com = calendar.components ([. Year,. Month,. Day], fromdate:self
Return com.year
}
/**
Get one months of time at a specified time
*/
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)!
}
/**
Get one months of time at a specified time
*/
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)!
}
/**
Gets the length of one months at a specified time
*/
Func getnextdatelenght ()->int {
Let date =self.getnextdate ()
Return Date.getmonthhowmanyday ()
}
/**
Gets the length of one months at a specified time
*/
Func getlastdatelenght ()->int {
Let date =self.getlastdate ()
Return Date.getmonthhowmanyday ()
}
Current Time Label contents
Func gettimeyyyy_mm ()->string {
Let Day =getday ()
Let month =getmonth ()
Let year =getyear ()
Let DateString =string ("Year" (month) month (day))
Return datestring
}
/**
Whether it is today
*/
Func istoday ()->bool {
Let Calendar =nscalendar.currentcalendar ()
Time to get self
Let comself = calendar.components ([. Year,. Month,. Day], fromdate:self
Get the current time
Let Comnow = calendar.components ([. Year,. Month,. Day], Fromdate:nsdate ()
return comself.year==comnow.year && comself.month==comnow.month && comself.day==comnow.day
}
/**
Whether it is this month
*/
Func isequalmonth (date:nsdate)->bool {
Let Calendar =nscalendar.currentcalendar ()
Time to get self
Let comself = calendar.components ([. Year,. Month,. Day], fromdate:self
Get the current time
Let Comnow = calendar.components ([. Year,. Month,. Day], Fromdate:date
return comself.year==comnow.year && Comself.month==comnow.month
}
/**
Whether it is this month
*/
Func isthismonth ()->bool {
Let Calendar =nscalendar.currentcalendar ()
Time to get self
Let comself = calendar.components ([. Year,. Month,. Day], fromdate:self
Get the current time
Let Comnow = calendar.components ([. Year,. Month,. Day], Fromdate:nsdate ()
return comself.year==comnow.year && Comself.month==comnow.month
}
/**
To get the exact date of the 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)
}
/**
Gets the specified 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)!
}
/**
Converts the current time to a string
*/
Func currentdateintostring ()->string {
Let Dateformatter =nsdateformatter ()
Dateformatter.dateformat = "Yyyy-mm-dd HH:mm:ss"
Let timestring = Dateformatter.stringfromdate (self)
Return timestring
}
}