使用按钮将日期添加2周

时间:2017-12-20 00:02:40

标签: ios swift date date-formatting days

好的,我一直试图解决这个问题几天。我是一个超级初学者,我有一个标签。在该标签中,我显示了日期1/20/21。我能够做到这一点。然后我有一个按钮。我希望每次点击该按钮都可以在14天内添加日期。而已。这是我的代码。请帮我。我不知道该写什么作为按钮的代码。我不明白如何使用这些方法以及括号中的内容。

import UIKit

class ViewController5: UIViewController {

    @IBOutlet weak var time: UILabel!

    @IBAction func ContButton(_ sender: UIButton) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .medium
        let someDateTime = Date(timeIntervalSinceReferenceDate: 632880000)

        time.text = dateFormatter.string(from: someDateTime)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

1 个答案:

答案 0 :(得分:1)

@IBAction func ContButton(_ sender: UIButton) {

               // let previousDate = "2017-06-29"
                  let previousDate = time.text
                let currentDate = Date()
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "yyyy-MM-dd"
                let previousDateFormated : Date? = dateFormatter.date(from: previousDate)
                let date = Calendar.current.date(byAdding: .day, value: 14, to: (previousDateFormated)! )
                print(date!)
                time.text = date

}

override func viewDidLoad() {
        super.viewDidLoad()
          let date = Date()
          let formatter = DateFormatter()
          formatter.dateFormat = "yyyy-MM-dd"
          let result = formatter.string(from: date)
          time.text = result
}