美文网首页
swift5 日期所在周开始日期 等

swift5 日期所在周开始日期 等

作者: 是我拉叔 | 来源:发表于2023-05-08 20:01 被阅读0次

    //日期所在周开始日期(星期天)

    funcstartOfWeek(ofdate:Date) ->Date? {

        letcalendar =NSCalendar.current

        letcomponents = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear],from: date)

        letstartOfWeek = calendar.date(from: components)

        returnstartOfWeek

    }

    //日期所在周结束日期(星期六)

    funcendOfWeek(ofdate:Date) ->Date? {

        guardletstartOfWeek = startOfWeek(of: date)else{returnnil}

        letcalendar =NSCalendar.current

        varcomponents =DateComponents()

        components.day=7

        components.second= -1

        letendOfWeek = calendar.date(byAdding: components,to: startOfWeek)

        returnendOfWeek

    }

    //日期所在月开始日期

    funcstartOfMonth(ofdate:Date) ->Date? {

        letcalendar =NSCalendar.current

        letcomponents = calendar.dateComponents([.year, .month],from: date)

        letstartOfMonth = calendar.date(from: components)

        returnstartOfMonth

    }

    //日期所在月结束日期

    funcendOfMonth(ofdate:Date) ->Date? {

        guardletstartOfMonth = startOfMonth(of: date)else{returnnil}

        letcalendar =NSCalendar.current

        varcomponents =DateComponents()

        components.month=1

        components.second= -1

        letendOfMonth = calendar.date(byAdding: components,to: startOfMonth)

        returnendOfMonth

    }

    //日期所在年开始日期

    funcstartOfYear(ofdate:Date) ->Date? {

        letcalendar =NSCalendar.current

        letcomponents = calendar.dateComponents([.year],from: date)

        letstartOfYear = calendar.date(from: components)!

        returnstartOfYear

    }

    //日期所在年结束日期

    funcendOfYear(ofdate:Date) ->Date? {

        guardletstartOfYear = startOfYear(of: date)else{returnnil}

        letcalendar =NSCalendar.current

        varcomponents =DateComponents()

        components.year=1

        components.second= -1

        letendOfYear = calendar.date(byAdding: components,to: startOfYear)

        returnendOfYear

    }

    functest() {

        guardletstart = startOfWeek(of:Date())?.timeIntervalSince1970else{return}

        print("start:\(start)")

        guardletend = endOfWeek(of:Date())?.timeIntervalSince1970else{return}

        print("end:\(end)")

        guardletstartOfMonth = startOfMonth(of:Date())?.timeIntervalSince1970else{return}

        print("startOfMonth:\(startOfMonth)")

        guardletendOfMonth = endOfMonth(of:Date())?.timeIntervalSince1970else{return}

        print("endOfMonth:\(endOfMonth)")

        guardletstartOfYear = startOfYear(of:Date())?.timeIntervalSince1970else{return}

        print("startOfYear:\(startOfYear)")

        guardletendOfYear = endOfYear(of:Date())?.timeIntervalSince1970else{return}

        print("endOfYear:\(endOfYear)")

    }

    相关文章

      网友评论

          本文标题:swift5 日期所在周开始日期 等

          本文链接:https://www.haomeiwen.com/subject/btblsdtx.html