iOS 時間

//    獲得年月上一個月的時間html

    func getYearMonth(dateFormatter:String) -> String {測試

        let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian).net

        let adcomps = NSDateComponents()code

        adcomps.year = 0component

        adcomps.month = -1orm

        adcomps.day = 0htm

        

        let newdate = calendar?.dateByAddingComponents(adcomps, toDate: NSDate(), options:.MatchFirst)get

        let formatter = NSDateFormatter()string

        formatter.dateFormat = dateFormatterio

        

        return formatter.stringFromDate(newdate!)

    }

 

//每調用一次月份向下跳一月,跨年則爲下一年1月

func getNextYearAndMonth () -> (year:Int,month:Int){

    var next_year:Int = currentYear

    var next_month:Int = currentMonth + 1

    if next_month > 12 {

        next_month=1

        next_year++

    }

    //Swift能夠同時返回多個值,返回值類型爲元組

    return (next_year,next_month)

}

 

//每調用一次月份向上跳一月,跨年則爲上一年12月

func getPrevYearAndMonth () -> (year:Int,month:Int){

    var prev_year:Int = currentYear

    var prev_month:Int = currentMonth - 1

    if prev_month == 0 {

        prev_month = 12

        prev_year--

    }

    return (prev_year,prev_month)

}

 

 

,獲得當前月第一天、最後一天日期

//本月開始日期
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))

相關文章
相關標籤/搜索