IOS-Swift 擷取當前日期 星期__IOS

來源:互聯網
上載者:User

        // 擷取當前日期

        let moonDay = Date().stringWithFormatterL(dateFormat: "M月dd日")

        let time = Date().stringWithFormatterL(dateFormat: "HH:mm")

        let weekDay = Date().weekDay()

        DLog("\(moonDay)------\(time)------\(weekDay)")



    // 擷取星期幾

    func weekDay() -> String {

        

        let weekDays = [NSNull.init(),"周日","周一","周二","周三","周四","周五","周六"] as [Any]

        

        let calendar = NSCalendar.init(calendarIdentifier: .gregorian)

        let timeZone = NSTimeZone.init(name: "Asia/Shanghai")

        calendar?.timeZone = timeZone as! TimeZone

        

        let calendarUnit = NSCalendar.Unit.weekday

        

        let theComponents = calendar?.components(calendarUnit, from: self)

        

        

        return weekDays[(theComponents?.weekday)!] as! String

        

    }

相關文章

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.