1, to get the first day of the current month, the last day date
This month's start date
Func startofcurrentmonth ()-> nsdate {
Let date = NSDate ()
Let calendar = Nscalendar.currentcalendar ()
Let components = Calendar.components ([. Year,. Month], fromdate:date)
Let Startofmonth = calendar.datefromcomponents (components)!
Return Startofmonth
}
Month End Date
Func Endofcurrentmonth (Returnendtime:bool = False)-> NSDate {
Let calendar = Nscalendar.currentcalendar ()
Let components = Nsdatecomponents ()
Components.month = 1
If Returnendtime {
Components.second =-1
} else {
Components.day =-1
}
Let Endofmonth = calendar.datebyaddingcomponents (components,
Todate:startofcurrentmonth (),
Options: [])!
Return Endofmonth
}
Test code:
Create a date format device
Let Dateformatter = NSDateFormatter ()
Dateformatter.dateformat = "yyyy year mm month DD Day HH:MM:SS"
Date of the first day of the month
Let StartDate = Startofcurrentmonth ()
Print ("Start of this month:", Dateformatter.stringfromdate (StartDate))
Date of the last day of the month 1
Let endDate1 = Endofcurrentmonth ()
Print ("End of this month 1:", Dateformatter.stringfromdate (endDate1))
Date of the last day of the month 2
Let EndDate2 = Endofcurrentmonth (True)
Print ("End of this month 2:", Dateformatter.stringfromdate (EndDate2))
2, the first day of the year, the date of the last day
Starting date of this year
Func startofcurrentyear ()-> nsdate {
Let date = NSDate ()
Let calendar = Nscalendar.currentcalendar ()
Let components = Calendar.components ([. Year], fromdate:date)
Let Startofyear = calendar.datefromcomponents (components)!
Return startofyear
}
Year End date
Func Endofcurrentyear (Returnendtime:bool = False)-> NSDate {
Let calendar = Nscalendar.currentcalendar ()
Let components = Nsdatecomponents ()
Components.year = 1
If Returnendtime {
Components.second =-1
} else {
Components.day =-1
}
Let Endofyear = calendar.datebyaddingcomponents (components,
Todate:startofcurrentyear (),
Options: [])!
Return endofyear
}
Test code:
Create a date format device
Let Dateformatter = NSDateFormatter ()
Dateformatter.dateformat = "yyyy year mm month DD Day HH:MM:SS"
Date of the first day of this year
Let StartDate = Startofcurrentyear ()
Print ("This year's start time:", Dateformatter.stringfromdate (StartDate))
Date of the last day of this year 1
Let endDate1 = Endofcurrentyear ()
Print ("Year end time 1:", Dateformatter.stringfromdate (endDate1))
Date of the last day of this year 2
Let EndDate2 = Endofcurrentyear (True)
Print ("Year end Time 2:", Dateformatter.stringfromdate (EndDate2))
3, the first day of the appointed year, the date of the last day
Start date of the specified month
Func startofmonth (Year:int, month:int)-> nsdate {
Let calendar = Nscalendar.currentcalendar ()
Let Startcomps = Nsdatecomponents ()
Startcomps.day = 1
Startcomps.month = Month
Startcomps.year = Year
Let StartDate = calendar.datefromcomponents (startcomps)!
return StartDate
}
End date of the specified month
Func endofmonth (Year:int, month:int, Returnendtime:bool = False)-> NSDate {
Let calendar = Nscalendar.currentcalendar ()
Let components = Nsdatecomponents ()
Components.month = 1
If Returnendtime {
Components.second =-1
} else {
Components.day =-1
}
Let Endofyear = calendar.datebyaddingcomponents (components,
Todate:startofmonth (Year,
month:month), Options: [])!
Return endofyear
}
Test code:
//Create a date formatter
let Dateformatter = NSDateFormatter ()
Dateformatter.dateformat = "yyyy year mm month DD Day HH:MM:SS"
&NBSP
//The first day of the month
Let StartDate = Startofmonth (2016, Month:8)
Print ("Start of August 2016:", Dateformatter.stringfromdate (startdate))
//month last day date 1
Let endDate1 = Endofmonth (2016, Month:8)
Print ("August 2016 end time 1:", Dateformatter.stringfromdate (endDate1))
//month last day date 2
Let EndDate2 = Endofmonth (2016, Month:8, returnendtime:true)
Print ("August 2016 End Time 2:", Dateformatter.stringfromdate (EndDate2))