使用按钮将日期增加 2 周
Using a button to add 2 weeks to a date
好的,这几天我一直在努力解决这个问题。我是一个超级初学者,我有一个标签。在该标签中,我显示了日期 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()
}
@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
}
好的,这几天我一直在努力解决这个问题。我是一个超级初学者,我有一个标签。在该标签中,我显示了日期 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()
}
@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
}