I want to make the Label display the date of today. The function itself from the date it wrote. Can't figure out how to embed in a UIcontroller?

@IBOutlet weak var CurrentDateLabel: UILabel!//Мой Label func CurrentDate(){ let date = Date() let MonthFormat = DateFormatter() MonthFormat.dateFormat = "MMMM" let Month = MonthFormat.string(from: date) let DayYearFormat = DateFormatter() DayYearFormat.dateFormat = "dd/yyyy" let DayYear = DayYearFormat.string(from: date) let DateArgument = (Month + "\n" + DayYear) CurrentDateLabel.text = DateArgument } 

No error, but nothing output

  • Can you add to the question text a function code that returns the required date? - Roman Podymov
  • In which method do you call CurrentDate ()? - 0rt

3 answers 3

 var dateFormatter = NSDateFormatter() dateFormatter.timeStyle = .MediumStyle var timeString = "Дата: \(dateFormatter.stringFromDate(NSDate()))" label1.text = timeString 
     // Создаем текущую дату let date = Date() // Создаем и настраиваем форматор дат let dateFormatter = DateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy" // формат даты для вывода 

    This service can help you in selecting the date format.

     //получаем строку из даты через formatter var dateString = dateFormatter.string(from: date) youLabel.text = dateString 

      In your UIViewController in the viewDidLoad() method write:

        let label = UILabel(frame: CGRect(x: 50, y: 50, width: 200, height: 100)) label.text = "Date property" view.addSubview(label)