-1

I would like to convert any time, UTC,, GMT+2 .. etc , anything to be only GMT +3

I tried this code but no success

        let date = Date()
        let formatter = DateFormatter()

        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
        formatter.timeZone = TimeZone.current

       let currentdate = formatter.string(from: date)

        print("currentdate \(currentdate)")


        let gmt = DateFormatter()
        gmt.timeZone = TimeZone(secondsFromGMT: 3600*3)
        gmt.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
        let gmtDate = gmt.date(from: currentdate)!

        print("gmtDate -> \(gmtDate)")

I tried abbreviations for the time zone, same result the date comes out to be only GMT.

Any ideas?

jscs
  • 63,694
  • 13
  • 151
  • 195
GramXGram
  • 1
  • 1
  • 1
  • Possible duplicate of [Getting date from \[NSDate date\] off by a few hours](https://stackoverflow.com/questions/8466744/getting-date-from-nsdate-date-off-by-a-few-hours) – jscs Oct 28 '17 at 20:19
  • Dates don't have time zones. When you print a date, it always picks GMT for display. – jscs Oct 28 '17 at 20:19
  • Your goal is unclear. What is your input (a `Date` or a `String`)? What is your desired output (a `Date` or a `String`)? Note that talking about timezones only makes sense when converting to/from `String`. – rmaddy Oct 28 '17 at 20:30

2 Answers2

4

Your code has a lot of issues. First, there is no reason to go from Date to String and back to Date. Second, if you are converting a String to a Date, and the String contains its own timezone information, then setting the formatter's timeZone is pointless. The timezone in the string will be used when calculating the associated Date. There are only two cases where setting a date formatter's timezone makes sense:

  1. When parsing a date/time string that does not contain any timezone information. The formatter's timezone will then be used to interpret the string.
  2. When converting a Date to a String. The formatter's timezone will be used when generating the resulting string from the date.

If you simply want to show any Date as a String in a specific timezone then all you need is:

let date = Date() // some date
print("Original Date (in GMT): \(date)")

// Setup a formatter with a date and time style
let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 3600 * 3) // the desired timezone
formatter.dateStyle = .long
formatter.timeStyle = .medium
let string = formatter.string(from: date)
print("GMT+3 style result: \(string)")

// For comparison, set the formatter to a specific format
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let string2 = formatter.string(from: date)
print("GMT+3 format result: \(string2)")

Output (for the en_US locale):

Original Date (in GMT): 2017-10-28 20:53:59 +0000
GMT+3 style result: October 28, 2017 at 11:53:59 PM
GMT+3 format result: 2017-10-28 23:53:59 +0300

There is no need to convert any time. Simply create a String from a Date to get the desired output.

Note that, by default, a DateFormatter shows its result in local time. Set the formatter's timeZone if you want the result in some other specific timezone.

Also note that printing a Date object always shows the date in UTC time (+0000). Many people get confused by this and think they are getting the wrong date when they are not.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • The last point here is key (I get this in bug reports a lot): when you print a date, it always displays in UTC because dates themselves have no associated time zone information. That does not mean it’s the wrong date — just that it is being interpreted differently. – Itai Ferber Oct 29 '17 at 07:18
  • hey guys , thanks for awesome answers one last thing @rmaddy i want have Date after convert, not a String because i would like to use that in Countdown, and it's require start date and end Date that what i want, convert any time to GMT 3 , because all dates of the data is in GMT 3 without converting, the countdown will give wrong results... thanks for awesome support again – GramXGram Oct 29 '17 at 17:54
  • If you already have a `Date` and you need a `Date` then there is nothing to convert. – rmaddy Oct 29 '17 at 17:59
  • Thanks Guys, and big thanks to rmaddy .... i got it working now very well... thanks again for awesome support – GramXGram Oct 29 '17 at 18:33
  • @GramXGram Glad to help. Just a reminder since you are new here, don't forget to accept answers that best solve your questions by clicking the checkmark to the left of the answer. This lets people know your question has been successfully answered. – rmaddy Oct 29 '17 at 18:36
1

Swift 5

private func convertDate(string:String) -> String {
        let dateFormatter = DateFormatter()
        // format in which the date and time comes from the server
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
         // set the time zone, the time is stored on the server
        dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3")
        // convert to Date
        let date = dateFormatter.date(from: string)

        // now set our local time zone
        dateFormatter.timeZone = TimeZone.current
        // time format we need
        dateFormatter.dateFormat = "dd.MM.yyyy HH:mm"
        // convert the Date obtained above into text format
        let displayString = dateFormatter.string(from: date!)
        // local time is ready)))
        return displayString
    }