Skip to content

Instantly share code, notes, and snippets.

@cicerohellmann
Last active April 29, 2021 14:56
Show Gist options
  • Save cicerohellmann/5c84c877f87273e85894cb59ca2fa265 to your computer and use it in GitHub Desktop.
Save cicerohellmann/5c84c877f87273e85894cb59ca2fa265 to your computer and use it in GitHub Desktop.
time conversion
let amPmFormat = "h:mm a"
let twentyFourHFormat = "HH:mm"
func hourMinuteParser(date: Date) -> KotlinInt{
let formatter = DateFormatter()
if DateFormatter.dateFormat(fromTemplate: "j",options:0, locale: Locale.current)!.contains("a") {
formatter.dateFormat = amPmFormat
}else{
formatter.dateFormat = twentyFourHFormat
}
let stringTime = formatter.string(from: date)
let time = formatter.date(from: stringTime)
formatter.dateFormat = "HH:mm"
let time24 = formatter.string(from: time!)
let timeWithoutSpecialCharacters = time24.replacingOccurrences(of: ":", with: "")
let int2 = Int32(timeWithoutSpecialCharacters) ?? 0
return KotlinInt(int: int2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment