Yogesh.Lolusare.Apple Yogesh.Lolusare.Apple - 5 months ago 22
iOS Question

GMT date string getting converted to UTC Date

enter image description here**


  1. GET GMT DATE STRING



**

func getGMTString(dateAsDate:NSDate) -> String
{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"//this your string date format
// dateFormatter.locale = NSLocale.currentLocale()
// dateFormatter.timeZone = NSTimeZone(name: "GMT")
let date = dateFormatter.stringFromDate(dateAsDate)

return date
}



  1. OUTPUT
    startDate---2016-06-29 00:00:00 GMT+5:30
    endDate----2016-06-30 03:57:39 GMT+5:30

  2. NOW TRYING to get GMT Date object from output string

    func getGMTDate(string:String) -> NSDate {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"//this your string date format
    // dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")!
    // dateFormatter.locale = NSLocale.currentLocale()
    let date = dateFormatter.dateFromString(string)


    return date!
    }


    PROBLEM: OUTPUT DATE OBJECT MESS
    startDateOBJECT---2016-06-28 18:30:00 +0000 endDateOBJECT----2016-06-30 17:30:00 +0000

  3. Unable to figure what is going wrong


Answer

I don't understand your step 3. I looks like you're using NSLog or a Swift print to display the resulting date. That is ALWAYS done in UTC.

If you want to view your date in a different format, or with your local time zone, you need a second date formatter to convert the NSDate to an output date string.

Here's the flow:

input date string -> input date formatter -> NSDate

NSDate -> output date formatter -> display date in local time zone