zoukankan      html  css  js  c++  java
  • NSCalendar--日历、日程、时间(二)

    1 获取一个月的天数

    func getNumberOfDaysInMonth() -> NSInteger {
        
            let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
            let range = calendar?.rangeOfUnit(.Day, inUnit: .Month, forDate: NSDate())
            return range?.length ?? 0
    }

    2 获取指定日期的年、月、日、时、分、秒、周几信息

    func getDateInfo() {
            
            let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
            let components = calendar?.components([.Year, .Month, .Day, .Hour, .Minute, .Second, .Weekday], fromDate: NSDate())
            
            let year = components?.year
            let month = components?.month
            let day = components?.day
            let hour = components?.hour
            let minute = components?.minute
            let second = components?.second
         let weekDay = components?.weekday
    
            print("year = (year)")
            print("month = (month)")
            print("day = (day)")
            print("hour = (hour)")
            print("minute = (minute)")
            print("second = (second)")
          print("weekDay = (weekDay)")  //1表示周日,2表示周一
          
        }

    3 计算两个时间之间的天数

    func numberOfDaysWithFromDate(date: NSDate, toDate: NSDate) -> NSInteger {
        
            let components = calendar?.components(.Day, fromDate: date, toDate: toDate, options: .WrapComponents)
            
            return components?.day ?? 0
        }
  • 相关阅读:
    bzoj 4548 小奇的糖果
    CF1151F Sonya and Informatics
    loj 2392「JOISC 2017 Day 1」烟花棒
    loj 2336「JOI 2017 Final」绳
    luogu P3620 [APIO/CTSC 2007]数据备份
    bzoj 4771 七彩树
    CF765F Souvenirs
    bzoj 3145 [Feyat cup 1.5]Str
    luogu P4482 [BJWC2018]Border 的四种求法
    第五章例题
  • 原文地址:https://www.cnblogs.com/muzijie/p/5802004.html
Copyright © 2011-2022 走看看