"epoch millis to hour of day. Like 7:23 PM, or 1:23 AM" in swift
- User
"epoch millis to hour of day. Like 7:23 PM, or 1:23 AM"
in swift
- Assistant
In Swift, we can convert epoch milliseconds to a human-readable hour of the day using
DateandDateFormatter. Here’s how you can do it:swiftimport Foundation func convertEpochMillisToHourOfDay(epochMillis: Int64) -> String { // Convert epoch milliseconds to seconds let date = Date(timeIntervalSince1970: TimeInterval(epochMillis / 1000)) // Set up date formatter let formatter = DateFormatter() formatter.timeZone = TimeZone.current // Set this to the desired timezone formatter.dateFormat = "h:mm a" // Hour:Minute with AM/PM // Get the hour of day let hourOfDay = formatter.string(from: date) return hourOfDay } // Usage example: let epochMillis: Int64 = 1649372587123 // Replace with your epoch time in milliseconds let hourOfDay = convertEpochMillisToHourOfDay(epochMillis: epochMillis) print(hourOfDay) // Output will be the hour of day like "7:23 PM" or "1:23 AM"