After formatting the date appears with 3 hours more

Asked

Viewed 184 times

0

earlier I had trouble transforming a String in date, with a lot of cost I managed to put in the exact time, but now when I went to generate the notification the blessed date appears with a few hours more, I did everything I thought it could be but I can’t find the solution

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

    if let unwrappedDate = date2 {
        print(dateFormatter.stringFromDate(unwrappedDate)) //26/09/2016 21:23
        criaNotificacoes(unwrappedDate, comIntervalo: util.valorIntervalo(campoIntervalo.text!), totalDias: util.valorTempoDias(campoPeriodo.text!))
    }else{
        print("tratar")
    }


}
func criaNotificacoes(dataInicio: NSDate, comIntervalo intervalo:Int, totalDias qtdDias:Int){
    print("data de inicio recebida --->",dataInicio)//2016-09-27 00:23:00
    let dateComponentes = NSDateComponents()
    let calendario = NSCalendar.currentCalendar()
    calendario.timeZone = NSTimeZone.localTimeZone()
    calendario.locale = NSLocale.currentLocale()
    dateComponentes.timeZone = NSTimeZone.localTimeZone()

    var fireDate: NSDate = NSDate()
    fireDate.descriptionWithLocale(NSLocale.currentLocale())

      fireDate = calendario.dateByAddingComponents(dateComponentes, toDate: dataInicio, options: NSCalendarOptions())!

    print(fireDate,"data inicio")//2016-09-27 00:23:00
}
  • Are you testing in the simulator or on a real device? If it is in the simulator it is possible that it is in another time zone. In this case, just go to Settings and adjust the spindle.

  • That on the iPhone itself :/

  • 2

    Nsdate does not have a Timezone, it is always GMT, what changes is the positioning of the user on the planet (Timezone). Whenever you print on Nsdate you are seeing the date with 3 hours more why here is GMT -3. Dateformatter by default uses local time zone, just use the Dateformatter stringFromDate method to see the local time (default) or if you want Dateformatter you can change the Dateformatter Timezone and show the time from another location.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.