如何从显示时间的文本字段中减去分钟
How to subtract minutes from a textfield displaying the time
我一直在研究如何执行此操作,但没有成功,我仍然不太了解 NSdate 和执行操作的方式。
我有一个应用程序,其中按下 timeText(UITextField) 会弹出 UIDatPicker,用户可以 select 使用滚轮所需的时间。然后,一旦按下 doneBTN(从工具栏),它就会填充该文本字段上所需的时间。
我要找的是分别在Tminus3(UILabel)、Tminus10(UILabel)、Tminus20(UILabel)上显示timeText(select来自用户的结果)减去3分钟( Tminus3), 负10分钟(Tminus10), 负20分钟(Tminus20) 分别在每个标签上。
即用户按下时间文本和 select 时间,比方说 11:30。当我按 updateBTN 我想获得 Tminus3.text = 11:27, Tminus10.text = 11:20, Tminus20.text = 11:10.[= 的值11=]
这是初始代码。
提前致谢
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var timeText: UITextField!
let timePicker = UIDatePicker()
@IBOutlet weak var Tminus3: UILabel!
@IBOutlet weak var Tminus10: UILabel!
@IBOutlet weak var Tminus20: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
createTimeP()
}
func creteToolB() -> UIToolbar {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneBTN = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneBTN], animated: true)
return toolbar
}
func createTimeP() {
timePicker.preferredDatePickerStyle = .wheels
timePicker.datePickerMode = .time
timePicker.locale = NSLocale(localeIdentifier: "UTC") as Locale
timePicker.minuteInterval = 5
timeText.inputView = timePicker
timeText.inputAccessoryView = creteToolB()
}
@objc func donePressed() {
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
timeFormatter.dateFormat = "HH:mm"
self.timeText.text = timeFormatter.string(from: timePicker.date)
self.view.endEditing(true)
}
@IBAction func **updateBTN**(_ sender: Any) {
// from the timeText.text = "HH:mm" get the value —> minus3.text(label) minutes, minus10.text()label minutes, minus20.text(label) minutes.
}
}
试试这个:
import UIKit
class tViewController: UIViewController {
@IBOutlet weak var timeText: UITextField!
let timePicker = UIDatePicker()
@IBOutlet weak var Tminus3: UILabel!
@IBOutlet weak var Tminus10: UILabel!
@IBOutlet weak var Tminus20: UILabel!
var selectedDate: Date? = nil
override func viewDidLoad() {
super.viewDidLoad()
createTimeP()
}
func creteToolB() -> UIToolbar {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneBTN = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneBTN], animated: true)
return toolbar
}
func createTimeP() {
timePicker.preferredDatePickerStyle = .wheels
timePicker.datePickerMode = .time
timePicker.locale = NSLocale(localeIdentifier: "UTC") as Locale
timePicker.minuteInterval = 5
timeText.inputView = timePicker
timeText.inputAccessoryView = creteToolB()
}
@objc func donePressed() {
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
timeFormatter.dateFormat = "HH:mm"
self.timeText.text = timeFormatter.string(from: timePicker.date)
self.selectedDate = timePicker.date
self.view.endEditing(true)
}
func getTime(date: Date) -> String {
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
timeFormatter.dateFormat = "HH:mm"
return timeFormatter.string(from: date)
}
@IBAction func updateBTN(_ sender: Any) {
if let selectedDate = self.selectedDate {
let tminus3Date = Calendar.current.date(byAdding: .minute, value: -3, to: selectedDate)!
let tminus10Date = Calendar.current.date(byAdding: .minute, value: -10, to: selectedDate)!
let tminus20Date = Calendar.current.date(byAdding: .minute, value: -20, to: selectedDate)!
self.Tminus3.text = getTime(date: tminus3Date)
self.Tminus10.text = getTime(date: tminus10Date)
self.Tminus20.text = getTime(date: tminus20Date)
}
}
}
我一直在研究如何执行此操作,但没有成功,我仍然不太了解 NSdate 和执行操作的方式。
我有一个应用程序,其中按下 timeText(UITextField) 会弹出 UIDatPicker,用户可以 select 使用滚轮所需的时间。然后,一旦按下 doneBTN(从工具栏),它就会填充该文本字段上所需的时间。
我要找的是分别在Tminus3(UILabel)、Tminus10(UILabel)、Tminus20(UILabel)上显示timeText(select来自用户的结果)减去3分钟( Tminus3), 负10分钟(Tminus10), 负20分钟(Tminus20) 分别在每个标签上。
即用户按下时间文本和 select 时间,比方说 11:30。当我按 updateBTN 我想获得 Tminus3.text = 11:27, Tminus10.text = 11:20, Tminus20.text = 11:10.[= 的值11=]
这是初始代码。
提前致谢
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var timeText: UITextField!
let timePicker = UIDatePicker()
@IBOutlet weak var Tminus3: UILabel!
@IBOutlet weak var Tminus10: UILabel!
@IBOutlet weak var Tminus20: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
createTimeP()
}
func creteToolB() -> UIToolbar {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneBTN = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneBTN], animated: true)
return toolbar
}
func createTimeP() {
timePicker.preferredDatePickerStyle = .wheels
timePicker.datePickerMode = .time
timePicker.locale = NSLocale(localeIdentifier: "UTC") as Locale
timePicker.minuteInterval = 5
timeText.inputView = timePicker
timeText.inputAccessoryView = creteToolB()
}
@objc func donePressed() {
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
timeFormatter.dateFormat = "HH:mm"
self.timeText.text = timeFormatter.string(from: timePicker.date)
self.view.endEditing(true)
}
@IBAction func **updateBTN**(_ sender: Any) {
// from the timeText.text = "HH:mm" get the value —> minus3.text(label) minutes, minus10.text()label minutes, minus20.text(label) minutes.
}
}
试试这个:
import UIKit
class tViewController: UIViewController {
@IBOutlet weak var timeText: UITextField!
let timePicker = UIDatePicker()
@IBOutlet weak var Tminus3: UILabel!
@IBOutlet weak var Tminus10: UILabel!
@IBOutlet weak var Tminus20: UILabel!
var selectedDate: Date? = nil
override func viewDidLoad() {
super.viewDidLoad()
createTimeP()
}
func creteToolB() -> UIToolbar {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneBTN = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneBTN], animated: true)
return toolbar
}
func createTimeP() {
timePicker.preferredDatePickerStyle = .wheels
timePicker.datePickerMode = .time
timePicker.locale = NSLocale(localeIdentifier: "UTC") as Locale
timePicker.minuteInterval = 5
timeText.inputView = timePicker
timeText.inputAccessoryView = creteToolB()
}
@objc func donePressed() {
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
timeFormatter.dateFormat = "HH:mm"
self.timeText.text = timeFormatter.string(from: timePicker.date)
self.selectedDate = timePicker.date
self.view.endEditing(true)
}
func getTime(date: Date) -> String {
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .short
timeFormatter.dateFormat = "HH:mm"
return timeFormatter.string(from: date)
}
@IBAction func updateBTN(_ sender: Any) {
if let selectedDate = self.selectedDate {
let tminus3Date = Calendar.current.date(byAdding: .minute, value: -3, to: selectedDate)!
let tminus10Date = Calendar.current.date(byAdding: .minute, value: -10, to: selectedDate)!
let tminus20Date = Calendar.current.date(byAdding: .minute, value: -20, to: selectedDate)!
self.Tminus3.text = getTime(date: tminus3Date)
self.Tminus10.text = getTime(date: tminus10Date)
self.Tminus20.text = getTime(date: tminus20Date)
}
}
}