Time and date formatter in swift 4





import UIKit

class TimeDateController: UIViewController {

    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var dateLabel: UILabel!
    
    var timer = Timer()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        updateTimer()
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)

        // Do any additional setup after loading the view.
    }

  
    
    @objc func updateTimer(){
        
        let timeFormatter =  DateFormatter()
        let dateFormatter = DateFormatter()
        
        timeFormatter.timeStyle = .medium
        dateFormatter.dateStyle = .medium
        
        timeLabel.text = timeFormatter.string(from: NSDate() as Date)
        dateLabel.text = dateFormatter.string(from: NSDate() as Date)
        
        
        
    }

}

Previous
Next Post »