Showing posts with label calendar. Show all posts
Showing posts with label calendar. Show all posts

Friday, February 24, 2017

Swift 3 Getting Age from DOB

If we have a functionality like user enters his/her DOB date and we need to calculate age or we are getting DOB string from service response and we need to calculate user’s age, Welcome to this post and see how to calculate age based on DOB.

In this post I am going to explain how to calculate age If we have a DOB string in
yyyy-MM-dd format.


  
 extension Date {
    var age: Int {
        return Calendar.current.dateComponents([.year], from: self, to: Date()).year!
    }
 }

 let strDOB = "1986-06-28" 

 let ageComponents = strDOB.components(separatedBy: "-") //["1986", "06", "28"]

 let dateDOB = Calendar.current.date(from: DateComponents(year:  
                                    Int(ageComponents[0]), month: Int(ageComponents[1]), day: 
                                    Int(ageComponents[2])))!   
                                                                 //Jun 28, 1986, 12:00 AM

 let myAge = dateDOB.age  //30


I have taken a simple Date extension where I am having age property which gives me number of years from current year technically. from is DOB date and to is current date.

My DOB string(1986-06-28), I am splitting by separator '-' and getting year, month and date strings, converting them to Ints and passing to Calendar's current date from components method to convert it to Date object and calling age property in my extension to get number of years gap from DOB date to current date which is technically user's age.

Hope this post is useful. Feel free to comment incase of any queries.


Friday, January 6, 2017

Swift 3 Adding all-day event to Device Calendar

If you have your contact’s Birthday date added while creating contact, Your iOS device Calendar shows you the Birthday reminder as all-day event as shown below.




In the same way, From our iOS app also we can add all-day events to the device calendar. In this post let me show you how can we do that using Swift.

  1. We need to import EventKit framework.
  2. We need to add privacy description for the in info.plist file.
  3. In my case it is Privacy - Calendars Usage Description (testing all-day event). This will be displayed to the user when OS asks for Calendar permissions to the user.


That’s it. Time for Swift code do add event to device Calendar

  
        
        let startDate = Calendar.current.startOfDay(for: Date())
        let store = EKEventStore()
        store.requestAccess(to: .event) {(granted, error) in
            if !granted {
                return
            }
            let event = EKEvent(eventStore: store)
            event.isAllDay = true
            event.title = "Business Meeting"
            event.notes = "Discussion on stocks."
            event.location = "New York City"
            event.startDate = startDate
            event.endDate = startDate
            event.calendar = store.defaultCalendarForNewEvents
            do {
                try store.save(event, span: .thisEvent, commit: true)
            } catch {
            }
        }
    }


If you see the above code,

We need to take the EventStore object
Request for access, which shows the below access request popup to the user.



If the request is granted, all we need to do is take EKEvent object and pass
  1. isAllDay - As we are adding all-day events, This value is true
  2. title - Title for event
  3. notes - Description of Event
  4. location - Where the event is going to happen
  5. startDate - Start date of the event (As it is all-day, both start and end dates are same)
  6. endDate - End date of the event 

That’s it. Save event using the store object we have created.

Now close the app and goto device calendar. You can see all-day event we have added as shown below.



Hope this post is useful. Feel free to comment in case of any queries.