Swift obtains the first day, the last day date code of the month, year, any months |

Source: Internet
Author: User
Tags dateformat

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.