Reputation: 569
I'm struct on how to get/convert current time to GMT time in format "Wed, 08 Apr 2015 21:27:30 GMT"
Currently I'm using below code to convert but that gives me "Wednesday" instead of "Wed" , "April" instead of "Apr" and most importantly in current local time.
Please advice how to get current GMT time in below format "Wed, 08 Apr 2015 21:27:30 GMT"
func convertDateToDayDateMonthYear(from inputFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = inputFormat
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let dateInLocal = dateFormatter.date(from: self) {
dateFormatter.dateFormat = "EEEE, dd MMMM yyyy"
dateFormatter.locale = Locale.current
return dateFormatter.string(from: dateInLocal)
}
return "NA"
}
Upvotes: 2
Views: 3198
Reputation: 39988
Use date formatter as
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
print(dateFormatter.string(from: date))
Console o/p
Tue, 10 Sep 2019 16:01:13 GMT
Follow this link to learn more about date formatting styles
Upvotes: 5
Reputation: 3064
Your dateFormatter.dateFormat is incorrect. Check this site to structure it the way you like: https://nsdateformatter.com/
This site will let you test out different formats without actually having to run code. This way, you'll be able to experiment and find the dateFormat that is best for your code.
Upvotes: 0