加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

斯威夫特 – 上周前一周的开始日和结束日

发布时间:2020-12-14 04:29:39 所属栏目:百科 来源:网络整理
导读:今天是3月6日星期五.如何找到2月16日是开始日,2月22日是前一周前一周的结束日. 16是我的国家保加利亚在美国将是15和21我使用.currentCalendar() 解决方法 这样的事情应该有效: let cal = NSCalendar.currentCalendar()let components = NSDateComponents()c
今天是3月6日星期五.如何找到2月16日是开始日,2月22日是前一周前一周的结束日.
16是我的国家保加利亚在美国将是15和21我使用.currentCalendar()

解决方法

这样的事情应该有效:

let cal = NSCalendar.currentCalendar()

let components = NSDateComponents()
components.weekOfYear -= 1

if let date = cal.dateByAddingComponents(components,toDate: NSDate(),options: NSCalendarOptions(0)) {
    var beginningOfWeek: NSDate?
    var weekDuration = NSTimeInterval()
    if cal.rangeOfUnit(.CalendarUnitWeekOfYear,startDate: &beginningOfWeek,interval: &weekDuration,forDate: date) {
        let endOfWeek = beginningOfWeek?.dateByAddingTimeInterval(weekDuration)
        print(beginningOfWeek) // Optional(2015-02-15 05:00:00 +0000)
        print(endOfWeek) // Optional(2015-02-22 05:00:00 +0000)
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读