1,得到當前月第一天、最後一天日期
//本月開始日期
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
}
//本月結束日期
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
}
測試代碼:
//創建一個日期格式器
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
//當月第一天日期
let startDate = startOfCurrentMonth()
print("本月開始時間:", dateFormatter.stringFromDate(startDate))
//當月最後一天日期1
let endDate1 = endOfCurrentMonth()
print("本月結束時間1:", dateFormatter.stringFromDate(endDate1))
//當月最後一天日期2
let endDate2 = endOfCurrentMonth(true)
print("本月結束時間2:", dateFormatter.stringFromDate(endDate2))
2,得到當年第一天、最後一天日期
//本年開始日期
func startOfCurrentYear() -> NSDate {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year], fromDate: date)
let startOfYear = calendar.dateFromComponents(components)!
return startOfYear
}
//本年結束日期
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
}
測試代碼:
//創建一個日期格式器
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
//本年度第一天日期
let startDate = startOfCurrentYear()
print("本年度開始時間:", dateFormatter.stringFromDate(startDate))
//本年度最後一天日期1
let endDate1 = endOfCurrentYear()
print("本年度結束時間1:", dateFormatter.stringFromDate(endDate1))
//本年度最後一天日期2
let endDate2 = endOfCurrentYear(true)
print("本年度結束時間2:", dateFormatter.stringFromDate(endDate2))
3,得到指定年月的第一天、最後一天日期
//指定年月的開始日期
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
}
//指定年月的結束日期
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
}
測試代碼:
//創建一個日期格式器
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
//當月第一天日期
let startDate = startOfMonth(2016, month: 8)
print("2016年8月的開始時間:", dateFormatter.stringFromDate(startDate))
//當月最後一天日期1
let endDate1 = endOfMonth(2016, month: 8)
print("2016年8月的結束時間1:", dateFormatter.stringFromDate(endDate1))
//當月最後一天日期2
let endDate2 = endOfMonth(2016, month: 8, returnEndTime: true)
print("2016年8月的結束時間2:", dateFormatter.stringFromDate(endDate2))