Develop an iPhone application that convert birth date into year, month and days in swift.

Code:

ViewController.swift:


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lblday: UILabel!
  
    @IBOutlet weak var view1: UIView!
    @IBOutlet weak var lblmonth: UILabel!
    @IBOutlet weak var lblyear: UILabel!
    @IBOutlet weak var txtdate: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        view1.isHidden = true
    }

    @IBAction func btnClicked(_ sender: Any) {
        view1.isHidden  = false
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM/yyyy"
        let date = dateFormatter.date(from: txtdate.text!)
        let NumOfDays: Int = daysBetweenDates(startDate: date!, endDate: Date())
        let year = NumOfDays/(365)
        let month =  (NumOfDays - (year * 365))/30
        let days = NumOfDays - (year * 365 + month * 30)
        lblyear.text = "\(year)"
        lblmonth.text = "\(month)"
        lblday.text = "\(days)"
    }
    func daysBetweenDates(startDate: Date, endDate: Date) -> Int {
        let calendar = Calendar.current
        let components = calendar.dateComponents([Calendar.Component.day], from: startDate, to: endDate)
        return components.day!
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Result:


0 comments:

Post a Comment