Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active May 1, 2023 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/8628a9fcd26e5d6821b14a59a573b767 to your computer and use it in GitHub Desktop.
Save sturdysturge/8628a9fcd26e5d6821b14a59a573b767 to your computer and use it in GitHub Desktop.
import SwiftUI
@available(iOS 16.0, *)
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
NavigationStack {
Form {
Section("Seconds since 1970") {
TextField("Timestamp", value: $viewModel.timestamp,
formatter: viewModel.timestampFormatter)
.keyboardType(.numberPad)
.onChange(of: viewModel.timestamp) { timestamp in
viewModel.date = Date(timeIntervalSince1970: timestamp)
}
}
Section("UTC Date") {
DatePicker("Select", selection: $viewModel.date)
.environment(\.timeZone, .gmt)
.onChange(of: viewModel.date) { date in
viewModel.timestamp = date.timeIntervalSince1970
}
}
Section("Local time zone") {
Text(TimeZone.current.identifier)
Text(viewModel.timeZoneOffsetString)
}
Section("Local Date") {
DatePicker("Select", selection: $viewModel.date)
}
}
.navigationTitle("Timestamp Converter")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment