Skip to content

Instantly share code, notes, and snippets.

@dimm0k
Created August 6, 2013 23:46
Show Gist options
  • Save dimm0k/6169936 to your computer and use it in GitHub Desktop.
Save dimm0k/6169936 to your computer and use it in GitHub Desktop.
AcidStrike's convert time to HH:mm
/*
Accepts most forms of Input as Time.
1, 1a, 1am, 100, 0100, 1:00, 1:00a, 1:00am, 01:00, 01:00am = 01:00
13, 1p, 1pm, 100p, 100pm, 0100p, 1:00p, 1:00pm, 01:00p, 01:00pm = 13:00
All forms of 12:00am = 00:00
*/
#Persistent
Gui, Add, Edit, vTime
Gui, Add, Edit, ReadOnly vTimeUpdate
Gui, Add, Button, Hidden Default, Ok
Gui, Show
return
ButtonOk:
Gui, Submit, NoHide
GuiControl,, TimeUpdate, % Time24(Time)
return
Time24(Time)
{
StringReplace, Time, Time, %A_Space%,,A
If (InStr(Time, "aa") = 1 || InStr(Time, "pp") = 1)
TimeOut := ""
else{
LenTime := StrLen(Time)
If (LenTime > 2 && SubStr(Time, 1, 2) > 12 && SubStr(Time, 2, 1) != ":" && SubStr(Time, 2, 1) != "a" && SubStr(Time, 2, 1) != "p"){
If !InStr(Time, ":"){
If (SubStr(Time, -1, 2) = "pm")
TimeOut := SubStr(SubStr(Time, 1, -2), 1, 2) . ":" . SubStr(SubStr(Time, 1, -2), 3, 2)
else If (SubStr(Time, 0, 1) = "p")
TimeOut := SubStr(SubStr(Time, 1, -1), 1, 2) . ":" . SubStr(SubStr(Time, 1, -1), 3, 2)
else
TimeOut := SubStr(Time, 1, 2) . ":" . SubStr(Time, 3, 2)
TimeOut .= ((LenTime = 3) ? ("00") :)
}else{
If (SubStr(Time, -1, 2) = "pm")
TimeOut := SubStr(SubStr(Time, 1, -2), 1, 2) . SubStr(SubStr(Time, 1, -2), 3, 3)
else If (SubStr(Time, 0, 1) = "p")
TimeOut := SubStr(SubStr(Time, 1, -1), 1, 2) . SubStr(SubStr(Time, 1, -1), 3, 3)
else
If (SubStr(Time, 0, 1) != "a")
TimeOut := Time
If (LenTime = 3)
TimeOut .= "00"
}}else If (LenTime = 1){
If Time in 1,2,3,4,5,6,7,8,9
TimeOut := Time . ":00"
}else If (LenTime = 2){
TimeOnly := SubStr(Time, 2, 1)
If TimeOnly is Digit
TimeOut := Time . ":00"
else If (TimeOnly = "a")
TimeOut := SubStr(Time, 1, 1) . ":00"
else If (TimeOnly = "p")
TimeOut := SubStr(Time, 1, 1) + 12 . ":00"
}else If (LenTime = 3){
If (SubStr(Time, 2, 2) = "am")
TimeOut := "0" . SubStr(Time, 1, 1) . ":00"
else If (SubStr(Time, 2, 2) = "pm")
TimeOut := SubStr(Time, 1, 1) + 12 . ":00"
else{
TimeOnly := SubStr(Time, 3, 1)
If TimeOnly is Digit
TimeOut := SubStr(Time, 1, 1) . ":" . SubStr(Time, 2, 2)
else If (TimeOnly = "a")
TimeOut := ((Time = "12a") ? ("00:00") : (SubStr(Time, 1, 2) . ":00"))
else If (TimeOnly = "p")
TimeOut := ((Time = "12p") ? (SubStr(Time, 1, 2) . ":00") : (SubStr(Time, 1, 2) + 12 . ":00"))
}}else If (LenTime = 4){
If InStr(Time, ":"){
StringSplit, TimeSplit, Time,:
If (StrLen(TimeSplit1) = 1)
TimeOut := Time
else If (StrLen(TimeSplit2) = 1)
TimeOut := TimeSplit1 . ":0" . TimeSplit2
}else{
TimeOnly := SubStr(Time, 3, 2)
If (TimeOnly = "am")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? ("00:00") : (SubStr(Time, 1, 2) . ":00"))
else If (TimeOnly = "pm")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? (SubStr(Time, 1, 2) . ":00") : (SubStr(Time, 1, 2) + 12 . ":00"))
else{
TimeOnly := SubStr(Time, 4, 1)
If TimeOnly is Digit
TimeOut := SubStr(Time, 1, 2) . ":" . SubStr(Time, 3, 2)
else If (TimeOnly = "a")
TimeOut := SubStr(Time, 1, 1) . ":" . SubStr(Time, 2, 2)
else If (TimeOnly = "p")
TimeOut := SubStr(Time, 1, 1) + 12 . ":" . SubStr(Time, 2, 2)
else
return, "ERROR"
}}}else If (LenTime = 5){
If InStr(Time, ":"){
TimeOnly := SubStr(Time, 5, 1)
If TimeOnly is Digit
TimeOut := Time
else If (TimeOnly = "a")
TimeOut := SubStr(Time, 1, 4)
else If (TimeOnly = "p")
TimeOut := SubStr(Time, 1, 1) + 12 . SubStr(Time, 2, 3)
}else{
TimeOnly := SubStr(Time, 4, 2)
If (TimeOnly = "am")
TimeOut := SubStr(Time, 1, 1) . ":" . SubStr(Time, 2, 2)
else If (TimeOnly = "pm")
TimeOut := SubStr(Time, 1, 1) + 12 . ":" . SubStr(Time, 2, 2)
else{
TimeOnly := SubStr(Time, 5, 1)
If (TimeOnly = "a")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? ("00:00") : (SubStr(Time, 1, 2) . ":" . SubStr(Time, 3, 2)))
else If (TimeOnly = "p")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? (SubStr(Time, 1, 2) . ":" . SubStr(Time, 3, 2)) : (SubStr(Time, 1, 2) + 12 . ":" . SubStr(Time, 3, 2)))
else
return, "ERROR"
}}}else If (LenTime = 6){
If InStr(Time, ":"){
If (SubStr(Time, 5, 2) = "am")
TimeOut := SubStr(Time, 1, 4)
else If (SubStr(Time, 5, 2) = "pm")
TimeOut := SubStr(Time, 1, 1) + 12 . SubStr(Time, 2, 3)
else If (SubStr(Time, 6, 1) = "a")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? ("00:00") : (SubStr(Time, 1, 5)))
else If (SubStr(Time, 6, 1) = "p")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? (SubStr(Time, 1, 5)) : (SubStr(Time, 1, 2) + 12 . SubStr(Time, 3, 3)))
}else{
If (SubStr(Time, 5, 2) = "am")
TimeOut := SubStr(Time, 1, 2) . ":" . SubStr(Time, 3, 2)
else If (SubStr(Time, 5, 2) = "pm")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? (SubStr(Time, 1, 2) . ":" . SubStr(Time, 3, 2)) : (SubStr(Time, 1, 2) + 12 . ":" . SubStr(Time, 3, 2)))
else
return, "ERROR"
}}else If (LenTime = 7){
If (SubStr(Time, 6, 2) = "am")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? ("00:00") : (SubStr(Time, 1, 5)))
else If (SubStr(Time, 6, 2) = "pm")
TimeOut := ((SubStr(Time, 1, 2) = 12) ? (SubStr(Time, 1, 5)) : (SubStr(Time, 1, 2) + 12 . SubStr(Time, 3, 3)))
else
return, "ERROR"
}else
return, "ERROR"
}
If (TimeOut = "")
return, "ERROR"
else If (SubStr(TimeOut, 2, 1) = ":")
TimeOut := "0" . TimeOut
return, TimeOut
}
GuiClose:
ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment