My code right now uses a date Picker to place a date and time in a textfield “dptext”. Thats all it does right now. I would like to be able to take whatever date and time is in dptext and match with current date of the users device. If both are the same print(“success”)
import UIKit
class ViewController: UIViewController {
@IBOutlet var dptext: UITextField!
let datePicker = UIDatePicker()
override func viewDidLoad() {
createDatePicker()
}
func createDatePicker() {
datePicker.datePickerMode = .dateAndTime
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)
dptext.inputAccessoryView = toolbar
dptext.inputView = datePicker
}
func donePressed() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
dptext.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
}}
You can compare two Date
objects using standard arithmetical operators, since they are implemented for this class.
if datePicker.date == Date() {
print("success")
}
The main issue here is that the Date
objects are compared to the seconds, and your UIDatePicker
returns a Date
with a second value of 0.
In order to performs precise arithmetical operations about Date
objects (such as : “are the date in the same month ?”, “what will be the date if I add 7080days to it?” etc.), you need to use a Calendar
object.
iOS 8 provided a lot of useful functions in this class, and I think the one you are looking for is isDateInToday()
:
if Calendar.current.isDateInToday(datePicker.date) {
print("success")
}
This is working. However what I am trying to do is is get it to print success if the date picker date and current time matches. So if the current time is 1:08 and I set the date picker at 1: 09 when the current becomes 1:09 success is not printed. Success is only printed when I selected a date at any time.
In this case, you have to use a Timer
object in order to perform the test continuously.
First, create a function for testing your date :
func testDate() {
if datePicker.date == Date() {
print("success")
}
}
Then create your timer object (in viewDidLoad
for example), create a variable in your class in order to keep a reference to it :
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController. testDate), userInfo: nil, repeats: true)
The time interval you choose for this timer must be lower or equal to 60 seconds, depending of the precision you want to achieve. The 10 seconds used here assure that you will detect a correct date within 10 sec after it changes.
You can also use testDate()
in donePressed()
in order to centralise your test in a single place.
This is all the code I have. If its 320 and I set the picker for 321. Nothing is printed when it becomes 321. Your help is much appreciated.
import UIKit
class ViewController: UIViewController {
var timer = Timer()
@IBOutlet var dptext: UITextField!
let datePicker = UIDatePicker()
override func viewDidLoad() {
createDatePicker()
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)
}
func createDatePicker() {
datePicker.datePickerMode = .dateAndTime
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)
dptext.inputAccessoryView = toolbar
dptext.inputView = datePicker
}
func testDate() {
if datePicker.date == Date() {
print("success")
}
}
func donePressed() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
dptext.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
}}
My bad, I forgot you wanted a test precise to the minute
func testDate() {
if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
print("success")
}
}
This Calendar
method is was you are looking for, according to Apple docs :
isDate(_:equalTo:toGranularity:)
Compares the given dates down to the given component, reporting them equal if they are the same in the given component and all larger components.
You should look at Apple Calendar documentation which is highly detailed.
This works how I want it to but when the datepicker and the current date match. The word success is printed 6 times instead one once within in the minute and have no idea why?
Since the timer keeps running even if you pass the test, then the test will pass 10 secondes later, 20, 30, 40, 50, then resulting to your 6 “success”.
You can either disable the timer once you pass the test, but it will not work if you change the date picker date andwait for the current date to be equal to the datePicker one, or keep a reference to the previous successful date :
var passingDate : Date?
func testDate() {
if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
print("success")
if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
{
// Previous date existing, and is in the same minute as the current date : do nothing
return
}
passingDate = datePicker.date
print("success")
}
}
1 Like