1

I stored a date in core data (as a date), and with the println it shows correctly its value: april 21 (is the var dateX below), but when right after the println i format it to string with the following code, the label linked to it shows april 22 (which is today, so i wonder tomorrow will show 23 etc.), where is the problem? anyone? thank you

if dateX != nil{
            var dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "MMM dd, yyyy"
            dateFormatter.timeZone = NSTimeZone.defaultTimeZone()
            var dateXstring = dateFormatter.stringFromDate(dateX as NSDate)
           startLabel.text = "Profile created on \(dateXstring)"
        }

println dateX and dateXstring:

enter image description here

my time zone is Rome (Italy)

m4tt
  • 825
  • 1
  • 11
  • 28
  • yea i saw the other threads so i've added the timeZone to the formatter but it doesn't seem to help idk why, i've edited with println and time zone, thanks – m4tt Apr 22 '15 at 18:28
  • It is the same problem as so often :) – NSDate does not have a timezone, and its description always uses UTC. `2015-04-21 22:53:34 +0000` is the same as `2015-04-22 00:53:34` in your local timezone (GMT+2). – Martin R Apr 22 '15 at 18:41

1 Answers1

1

You likely have a timezone issue. Where are you located? DefaultTimeZone could be GMT/ZULU time which is -5 hrs from the east coast.

A good way to check is to use the timeIntervalSince1970 function (i think thats what it is called). If the stored date and retrieved date have the same value its the same date and you have a display problem.

timeIntervalSince1970 returns a NSTimeInterval which is really a Double

Jeef
  • 26,861
  • 21
  • 78
  • 156