convert gmt to eastern time in swift

To convert GMT to Eastern Time in Swift, you can use the DateFormatter and TimeZone classes. Here's an example code snippet:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" // Set the date format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT") // Set GMT as timezone
let date = "2021-09-12 10:00:00")! // Create a date object from a string

dateFormatter.timeZone = TimeZone(abbreviation: "EST") // Set Eastern Time as timezone
let easternTime = dateFormatter.string(from: date) // Convert date to string in Eastern Time

print(easternTime) // Output: "2021-09-12 06:00:00"
518 chars
10 lines

In the code above, we first create a DateFormatter object and set the date format to match the format of the input string. Then, we set the timezone of the formatter to GMT and create a Date object from the input string.

Next, we set the timezone of the formatter to Eastern Time (EST) and use the formatter to convert the Date object to a string in Eastern Time. Finally, we print the resulting string to the console.

Note that the timezone abbreviations used here (GMT and EST) are just examples and you should use the correct abbreviations for your specific timezone.

gistlibby LogSnag