To convert a Date
from GMT to Pacific Time in Swift, you can use the TimeZone
class and its init(identifier:)
method to create the two time zone objects. Then use these to create the dateFormatter object to format the dates.
main.swift477 chars16 lines
In the code above, we:
TimeZone
objects, one for GMT and one for Pacific Time.Date
object for the current date and time.DateFormatter
object with a format string that we can use to create a string representation of the dates.timeZone
property of the dateFormatter
to the GMT time zone and generate a string representation of the date in GMT.timeZone
property of the dateFormatter
to the Pacific Time time zone and generate a string representation of the date in Pacific Time.You can modify the dateFormat
and timeZone
properties of the DateFormatter
object to fit your needs.
gistlibby LogSnag