# Create alarm alert with UIDatePicker (swift3)

**URL:** <https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082>\
**Category:** Swift\
**Created:** [June 18, 2017, 9:08pm UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082 "2017-06-18T21:08:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [June 18, 2017, 9:08pm UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/1 "2017-06-18T21:08:17Z")

</div>

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)

      }}

```

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [June 19, 2017, 7:50am UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/2 "2017-06-19T07:50:35Z")

</div>

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")
}

```

---

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [June 19, 2017, 5:12pm UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/3 "2017-06-19T17:12:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [June 20, 2017, 7:14am UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/4 "2017-06-20T07:14:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [June 20, 2017, 7:22pm UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/5 "2017-06-20T19:22:25Z")

</div>

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)

      }}

```

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [June 21, 2017, 7:10am UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/6 "2017-06-21T07:10:49Z")

</div>

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](https://developer.apple.com/documentation/foundation/calendar) which is highly detailed. 🙂

---

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [June 21, 2017, 6:15pm UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/7 "2017-06-21T18:15:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [June 22, 2017, 7:06am UTC](https://forums.kodeco.com/t/create-alarm-alert-with-uidatepicker-swift3/31082/8 "2017-06-22T07:06:54Z")

</div>

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")
    }
}

```
