0

I'm trying to play with dates, saving them as strings, then returning them to dates. my output loses a day. Please see below for the playground code:

 let date = NSDate()
 let formatter = NSDateFormatter()
 let secondDateFormatter = NSDateFormatter()
 formatter.dateStyle = .FullStyle
 let dateString = formatter.stringFromDate(date)

 let dateFormatter = NSDateFormatter()
 dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"

 let finalDate : NSDate = dateFormatter.dateFromString(dateString)!
 println(finalDate)

here's the current output:

date = May 7, 2015, 9:16 AM 
dateString = "Thursday, May 7, 2015" (perfect) 
let finalDate shows: "May 7, 2015, 12:00 AM" (almost perfect except the time) 
println(finalDate) reveals : 2015-05-06 16:00:00 +0000 

I've searched around and read that it is a time zone modification? I'm not sure. I'll play with it more and see if it works for my needs. Any idea why the output would be different when println is execute vs. just the calculation?

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Mason Ballowe
  • 1,847
  • 2
  • 12
  • 23
  • 1
    Checkout this question: http://stackoverflow.com/questions/29407599/nsdateformatter-return-wrong-date-swift/29409739 – Abdullah May 07 '15 at 01:31
  • You've only lost 8 hours, not an entire day. – nhgrif May 07 '15 at 01:35
  • 1
    println doesn't take into account of the time zone, so it prints the equivalent time in GMT. – Schemetrical May 07 '15 at 01:53
  • [Don't confuse the point in time (NSDate) with the representation of that point at your location (the string from the NSDateFormatter](http://stackoverflow.com/questions/4976530/returns-a-date-an-hour-in-the-future/4976950#4976950) – Matthias Bauch May 07 '15 at 06:27

0 Answers0