【问题标题】:ios: Get 3 weeks (by date) aheadios:提前 3 周(按日期)
【发布时间】:2016-02-16 11:49:44
【问题描述】:

我需要从当前日期提前 3 周。并将所有日期添加到数组中。 我怎样才能得到这个?

 let date = NSDate()
        let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
        let comps = calendar?.components([.Day, .WeekOfMonth, .Month], fromDate: date)
        let days = calendar?.rangeOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.Month, forDate: date)
        print(days?.length)

这段代码给了我当月的天数。但下个月可能会出现 3 个弱点

例如今天是 16.02 我需要打印这样的东西

16 17 18 19 20 21 22 23 24 25 26 27 28 29 01 02 04....

感谢提前

【问题讨论】:

  • 此解决方案不包括夏令时
  • func dateByAddingComponents(_ comps: NSDateComponents, toDate date: NSDate, options opts: NSCalendarOptions) -> NSDate?
  • 我投票结束这个问题作为离题,因为没有研究工作。请阅读文档 :)
  • Daij-Djan 我的答案怎么样?问题的答案好吗?

标签: ios swift date nsdate nscalendar


【解决方案1】:

怎么样,它会根据要求生成一个日期数组

    var date = NSDate()
    var days : [String] = []

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd"

    for _ in 0...20
    {
        days.append(dateFormatter.stringFromDate(date))

        // move on to the next day
        date = NSCalendar.currentCalendar().dateByAddingUnit(
            .Day,
            value: 1,
            toDate: date,
            options: NSCalendarOptions(rawValue: 0))!
    }
    print(days)

【讨论】:

  • 是的,我写了同样的 :) 看到我更新的答案 :) 但谢谢你。
【解决方案2】:

好的,找到了一些解决方案。据我所知,每周 7 天 * 3 = 21

   // MARK: - Next 3 weeks
    func nextThreeWeeks() -> Array<String> {
        let date = NSDate()
        var days : Array<String> = []
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
        dateFormatter.dateFormat = "dd"
        let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)

        for i in 0 ..< 21 {
            let next21Days = calendar!.dateByAddingUnit(NSCalendarUnit.Day, value: i, toDate: date, options: [])
            days.append(dateFormatter.stringFromDate(next21Days!))
        }
        return days
    }

它将打印以下日期:

可选(2016-02-16 12:03:41 +0000)
可选(2016-02-17 12:03:41 +0000)
可选(2016-02-18 12:03:41 +0000)
可选(2016-02-19 12:03:41 +0000)
...
可选(2016-03-06 12:03:41 +0000)
可选(2016-03-07 12:03:41 +0000)

【讨论】:

    【解决方案3】:

    我得到了您所要求的打印结果的解决方案。

    下面的代码和输出

      var arrayDate = [String]()
    
      override func viewDidLoad()
      {
        super.viewDidLoad()
        for var intWeek = 0; intWeek < 21; ++intWeek
        {
            let formatter = NSDateFormatter()
            formatter.dateFormat = "dd"
            let dateComponents: NSDateComponents = NSDateComponents()
            dateComponents.day = intWeek
            let date = NSDate()
            let calendar = NSCalendar.currentCalendar()
            let components = calendar.dateByAddingComponents(dateComponents, toDate: date, options:NSCalendarOptions(rawValue: 0))
            let startOfDay = formatter.stringFromDate(components!)
            arrayDate.append(startOfDay)
            print("The dates are - \(arrayDate)")
        }
      }   
    

    输出结果

    The dates are - ["16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "01", "02", "03", "04", "05", "06", "07"]
    

    非常感谢安东

    【讨论】:

    • 谢谢,已经发布了自己的答案。它会帮助别人
    • 我的答案和你的两个答案都不一样。
    猜你喜欢
    • 1970-01-01
    • 2020-06-07
    • 2020-12-16
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    相关资源
    最近更新 更多