daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Mar 25th, 2019 135 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.swift
  3. //  DatePicker
  4. //
  5. //  Created by Marko Poikkimäki on 2019-03-20.
  6. //  Copyright © 2019 Marko Poikkimäki. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.  
  13.     @IBOutlet weak var datePicker: UIDatePicker!
  14.    
  15.     @IBOutlet weak var label: UILabel!
  16.    
  17.    
  18.     override func viewDidLoad() {
  19.         super.viewDidLoad()
  20.         // Do any additional setup after loading the view, typically from a nib.
  21.        
  22.         datePicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)
  23.         datePicker.datePickerMode = .dateAndTime
  24.     }
  25.    
  26.     @objc
  27.     func dateChanged(_ sender: UIDatePicker){
  28.        
  29.         let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: sender.date)
  30.         if let day = components.day, let month = components.month, let year = components.year, let hour = components.hour, let minutes = components.minute  {
  31.  
  32.             label.text = "Chosen date: \(year)-\(month)-\(day) \(hour):\(minutes)"
  33.         }
  34.        
  35.     }
  36.  
  37.  
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top