Using Dateformatter with Timezone to Format Dates in Swift

Date Format in Swift

You have to declare 2 different NSDateFormatters, the first to convert the string to a NSDate and the second to print the date in your format.

Try this code:

let dateFormatterGet = NSDateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"

let dateFormatterPrint = NSDateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"

let date: NSDate? = dateFormatterGet.dateFromString("2016-02-29 12:24:26")
print(dateFormatterPrint.stringFromDate(date!))

Swift 3 and higher:

From Swift 3 NSDate class has been changed to Date and NSDateFormatter to DateFormatter.

let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"

let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"

if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26") {
print(dateFormatterPrint.string(from: date))
} else {
print("There was an error decoding the string")
}

How to format a date using timezone in Swift?

The format you want is hh:mm a Z, which will provide the +0800.

You want to create a TimeZone which +8 hours from GMT, normally I prefer to use the appropriate abrivations (ie AET), but I guess if you don't have that, you can create a TimeZone using secondsFromGMT, for example...

let tz = TimeZone(secondsFromGMT: 8 * 60 * 60)
let toFormatter = DateFormatter()
toFormatter.timeZone = tz
toFormatter.dateFormat = "hh:mm a Z"
let requiredTime = toFormatter.string(from: convertedDate!)

Which based on your example data, will produce a value of...

05:00 AM +0800

How can I convert including timezone date in swift?

Xcode 8 • Swift 3

You need to escape 'T' and use the appropriate timezone symbol. Note that it will depend if your date string represents a UTC (zero seconds from GMT) time timezone with Z "XXXXX" or without it +00:00 "xxxxx":

let dateString = "2015-08-14T20:02:25-04:00"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"
if let date = formatter.date(from: dateString) {
formatter.dateFormat = "yyyy-MM-dd HH:mm"
let string = formatter.string(from: date)
print(string)
}

If you need some reference you can use this:

Sample Image

how to change date format 2021-30-06T05:00:00+07:00 in swift?

Your 2021-30-06T05:00:00+07:00 is not matching with yyyy-MM-dd'T'HH:mm:ss.

Date format should be yyyy-dd-MM'T'HH:mm:ssZZZZZ.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-dd-MM'T'HH:mm:ssZZZZZ"
let date = dateFormatter.date(from: "2021-30-06T05:00:00+07:00")

// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
let timeStamp = dateFormatter.string(from: date!)
print(timeStamp)

Output:

03:30:00  // This is as per my current timezone.

Swift Dateformatter() converts wrong date

You need yyyy not YYYY

dateFormatter.dateFormat = "dd-MM-yyyy-HH:mm"


Related Topics



Leave a reply



Submit