0

I'm working with an app thau use NSDate, it's an app that will be only available for people who has more tha 13 years, so what I want to do is setup the date from the actual date, I mean today but I want to setup the year to 2004 so when I launch the app should appear the actual date, the actual month but the year should be 2004. Right now if the user select a date under 2004 an alert appears, I want to disable this alert and setup the 2004 as default year and then be able to move to any year that the user wants. I'm working with I let the code to validate the year and the code to set the current date, hope you can help me

func validateDate () {

        let date = Calendar.current.date(byAdding: .year, value: -13, to: Date())

        if selectedDate == nil {
            selectedDate = Date()
        }





func changeDateToString (date:Date) -> String {

    let calendar = Calendar.current
    let comp = calendar.dateComponents([.month, .day], from: date)

    let year : Int = comp.year!
    let month : Int = comp.month!
    let day : Int = comp.day!


    var stringDay: String = "\(day)"
    var stringMonth: String = "\(month)"
    let stringYear: String = "\(year)"

    if stringDay.characters.count == 1 {
        stringDay.insert("0", at: stringDay.startIndex)
    }

    if stringMonth.characters.count == 1 {
        stringMonth.insert("0", at: stringMonth.startIndex)
    }



    let finalString = stringYear + stringMonth + stringDay + " "

    print(finalString)

    return finalString
}
Del
  • 121
  • 1
  • 7
  • http://stackoverflow.com/a/31418374/2303865 – Leo Dabus Mar 01 '17 at 17:49
  • Ok sí, que estaba haciendo eso, pero yo quiero en la vista del calendario aparece el día actual, el mes en curso y el año 2004 como el año por defecto, pero cuando ejecuto la aplicación nada no aparece el año 2004 como mi año por defecto, aparece la fecha de hoy y el año 2017 – Del Mar 01 '17 at 17:54
  • What you mean default year? `let comp = calendar.dateComponents([.month, .day], from: date)` you are not getting the year component – Leo Dabus Mar 01 '17 at 17:55
  • Yes when I setup the current date obviously appears the actual day, month and year in the picker. So what I want is display the current day, month but the year should be 2004 every time that I launch my app and then I can move in the picker normally to select any year that I want. – Del Mar 01 '17 at 18:02
  • `datePicker.date = Calendar.current.date(byAdding: .year, value: -16, to: Date())` – Leo Dabus Mar 01 '17 at 18:03
  • As you can see in my first method called validate date, I'm doing this but nothing is changing in the picker still appearing 2017 as the year when should be 2004 – Del Mar 01 '17 at 18:07
  • I can't see you setting the datePicker's date property anywhere in your code – Leo Dabus Mar 01 '17 at 18:29
  • 1
    Yes, I wasn't setting up at that point I've already did it, and now it's working properly. Thanks for your help – Del Mar 01 '17 at 19:54

0 Answers0