To convert Samoa time (SST) to Greenwich Mean Time (GMT) in Swift, follow these steps:
Create instances of DateFormatter
and Date
classes.
Set the timeZone
property of the DateFormatter
instance to the Samoa timezone (Pacific/Apia
).
Set the dateFormat
property of the DateFormatter
instance to the appropriate date format string that matches the input date's format.
Create a string that represents the Samoa time.
Use the date(from:)
method of the DateFormatter
instance to convert the string to a Date
object.
Create a Calendar
instance and set its timeZone
property to the GMT timezone (GMT
).
Use the dateComponents(in:)
method of the Calendar
instance to get the date and time components of the Samoa time in the GMT timezone.
Create a new string from the GMT time components using a DateFormatter
instance with the appropriate date format string.
Here's the Swift code for this:
main.swift749 chars20 lines
This code assumes that the input Samoa time is in the format "yyyy-MM-dd HH:mm:ss". You may need to adjust the dateFormat
property of the DateFormatter
instance to match your specific input format.
gistlibby LogSnag