Skip to content

Instantly share code, notes, and snippets.

@TheDcoder
Last active October 3, 2017 11: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 TheDcoder/57927dea2344447bfd24b0e359b49ac3 to your computer and use it in GitHub Desktop.
Save TheDcoder/57927dea2344447bfd24b0e359b49ac3 to your computer and use it in GitHub Desktop.
12 hour to 24 hour format in AutoIt
; #FUNCTION# ====================================================================================================================
; Name ..........: ConvertTime12To24
; Description ...: Converts 12 hour time to 24 hour time, made while keeping _DateDiff's format in mind :)
; Syntax ........: ConvertTime12To24($sTime, $sPeriod[, $sDelimiter = ':'])
; Parameters ....: $sTime - Timestamp. (Can be a single digit or a pattern like H:M:S)
; $sPeriod - AM or PM.
; $sDelimiter - [optional] Separator between units of time. Default is ':'.
; Return values .: $sTime in 24 hour format with 0 padding
; Author ........: Damon Harris (TheDcoder)
; Remarks .......: Ensures that the first 3 parts have 0 padding if the time unit is single digit (Example 09 instead of 9)
; Link ..........: https://git.io/vdWnY
; Example .......: ConvertTime12To24('8:07:22', 'PM')
; ===============================================================================================================================
Func ConvertTime12To24($sTime, $sPeriod, $sDelimiter = ':')
$aTime = StringSplit($sTime, ':')
For $iElement = 1 To ($aTime[0] > 3 ? 3 : $aTime[0])
$aTime[$iElement] = StringFormat('%.2i', Number($aTime[$iElement]))
Next
If $sPeriod = 'PM' Then $aTime[1] = StringFormat('%.2i', Number($aTime[1]) + 12)
Return _ArrayToString($aTime, $sDelimiter, 1)
EndFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment