Date picking time from another time zone

0

I have a big problem, I need to store the date and time selected by the user so that I can generate the notifications at the correct time. The date and time is captured by a date picker and when I get it as String it comes in the correct way "Sep 27 2016 13:04", but when I do the conversion to NSDate the time is zapped, 2016-09-27 16 : 04. Can anyone help solve this problem? I'm doing the tests with the iPhone and not on the simulator

let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy HH:mm"
    dateFormatter.locale = NSLocale.currentLocale()
    dateFormatter.timeZone = NSTimeZone.localTimeZone()
    let date2:NSDate!
        date2 = dateFormatter.dateFromString(dateStr4)
    dateFormatter.dateFormat = "dd/MM/yyyy HH:mm"

    if let unwrappedDate = date2 {

        print(dateFormatter.stringFromDate(unwrappedDate))// aqui aparece a data e hora de maneira correta 27/09/2016 13:09
        print("--->",unwrappedDate) // aqui a hora já fica zoada 27/09/2016 16:09
        criaNotificacoes(unwrappedDate, comIntervalo: util.valorIntervalo(campoIntervalo.text!), totalDias: util.valorTempoDias(campoPeriodo.text!))
    }else{
        print("tratar erro ")
    }
    
asked by anonymous 27.09.2016 / 18:11

1 answer

0

You are giving print, but you are not saving the correct date in any constant. Try to save dateFormatter.stringFromDate(unwrappedDate) to a constant and call it in code instead of unwrappedDate .

More or less like this:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy HH:mm"
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let date2:NSDate!
    date2 = dateFormatter.dateFromString(dateStr4)
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm"

if let unwrappedDate = date2 {
    let data = dateFormatter.stringFromDate(unwrappedDate)
    print(data)
    print("--->", data)
    criaNotificacoes(data, comIntervalo: util.valorIntervalo(campoIntervalo.text!), totalDias: util.valorTempoDias(campoPeriodo.text!))
}else{
    print("tratar erro ")
}
    
29.09.2016 / 16:10