0

I am trying to format an existing date in my project. Also, I am using localTimeZone to format date. However, it returns UTC and does not give correct date. Here is my code:

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
bagItem.date = dateFormatter.dateFromString(dateFormatter.stringFromDate(self.selectedDateForAdmin))

After the code above during debugging, for instance; today is 21-04-2015. However, it returns 20-04-2015. The actual question is that Why NSTimezone.localTimeZone() does not return correct date? Also, why returns UTC? I live in Turkey and timezone should be GMT. Please help me. This is a huge problem for my project.

Best regards

saksut
  • 673
  • 1
  • 7
  • 14
  • NSDate **does not have a time zone**. Its description **always use UTC**. (There are *many* similar questions with answers). If you want to present the date in the local timezone then you should convert the result of `stringFromDate()` back to an NSDate. – Martin R Apr 21 '15 at 11:37
  • So, what is the best way to format a date for current local date and time ? @MartinR – saksut Apr 21 '15 at 11:42
  • `dateFormatter.stringFromDate(self.selectedDateForAdmin)` is already a formatted string in the local time zone. – Martin R Apr 21 '15 at 11:45
  • Ok I got it. However I need to date as a NSDate type not string ? How can I get the date from formatted date with "stringFromDate" as correct date @MartinR – saksut Apr 21 '15 at 11:58
  • NSDate describes an absolute point in time. Internally it is stored just as the number of seconds since Jan 1, 2001 GMT. NSDate does not know anything about time zones. So "NSDate in the local timezone" makes no sense! – Martin R Apr 21 '15 at 12:03

0 Answers0