gpt4 book ai didi

swift - 在 Swift 中查找日期之间的时间

转载 作者:行者123 更新时间:2023-11-28 07:08:54 24 4
gpt4 key购买 nike

<分区>

嗨,我正在尝试使用 UIpickers 计算两个日期之间的时间,但我迷路了。我不知道该去哪里才能获得插入标签的值(value)。我不确定我应该使用组件还是在比较之前或之后将其放入标签中。感谢您的指点。

let calendar = NSCalendar.currentCalendar()
let userCalendar = NSCalendar.currentCalendar()

// Set up date object
let DateMakerFormatter = NSDateFormatter()

let date = NSDate()

@IBOutlet weak var toDatePicker: UIDatePicker!
@IBOutlet weak var fromDatePicker: UIDatePicker!
@IBOutlet weak var dayYearLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func toDatePickerAction(sender: UIDatePicker) {
var DateFormatter = NSDateFormatter()
DateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
var endTime = DateFormatter.stringFromDate(toDatePicker.date)
self.dayYearLabel.text = endTime
var targetDate = endTime
println("\(endTime)")
}

@IBAction func fromDatePickerAction(sender: UIDatePicker) {
var fromDateFormatter = NSDateFormatter()
fromDateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
var startTime = fromDateFormatter.stringFromDate(fromDatePicker.date)
self.dayYearLabel.text = startTime
var targetDate2 = startTime
println("\(startTime)")
}

@IBAction func getTheDateButton(sender: AnyObject) {
let calendar = NSCalendar.currentCalendar()
let datecomponents = calendar.components(NSCalendarUnit.CalendarUnitSecond,
fromDate: startTime, toDate: endTime, options: nil)
let second = datecomponents.second
let minute = datecomponents.minute
let hour = datecomponents.hour
let day = datecomponents.day
let year = datecomponents.year


println("Seconds: \(second)")
println("minutes: \(minute)")
println("hours: \(hour)")
println("days: \(day)")
println("years: \(year)")

}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com