;; 
;;====================================================================== 
;; 
;;FUNCTION       TimeFix() 
;; 
;;ACTION         Reformats Time Values to YYYY/MM/DD HH:MM:SS 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.2  - 2020/07/02 
;; 
;;HISTORY        1.0  - 2018/01/12 - Initial Release 
;;               1.1  - 2020/04/08 - auto-detect YMD and MDY time formats, add time as 
;;                                  "00:00:00" if not specified. 
;;               1.2  - 2020/07/02 - Add option to suppress time value 
;; 
;;SYNTAX         TimeFix(DateString [, SrcFormat] [, NoTime]) 
;; 
;;PARAMETERS     DateString - REQUIRED - A date/time string in any format or order. 
;;                            Can be "TIME DATE" or "DATE TIME". Assumes that TIME 
;;                            value is always ordered in H:M:S. Defaults the DATE  
;;                            format to M/D/Y or M-D-Y. The order of Month, Day, and 
;;                            Year can be defined by providing the SrcFormat value. 
;;                            If no time part is specified, it defaults to midnight. 
;; 
;;               SrcFormat  - OPTIONAL - Defines the sequence of Month, Day, and  
;;                            Year data values as a 3-character string. To specify 
;;                            a Day/Month/Year sequence, provide "DMY" as the value. 
;;                            Any sequence of "Y", "M", and "D" can be specified to 
;;                            match the order of data in the original DATE string. 
;; 
;;               NoTime     - OPTIONAL - Boolean that when true, returns only the date. 
;; 
;;REMARKS        Used to return various time formats to a format used universally 
;;               by Kixtart and most Kix time-related functions. It also handles 
;;               conversion of 12-hour AM/PM to 24-hour time format and zero-padding. 
;; 
;;RETURNS        timestamp in YYYY/MM/DD HH:MM:SS format 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K8, W2K12, W2K16, Win-7/8/10 
;; 
;;EXAMPLES       $Time = $File.time          ; get file time from FSO - M/D/Y H:M:S a/p format 
;;               $Time = TimeFix($Time)      ; returns YYYY/MM/DD HH:MM:SS format 
; 
Function TimeFix($_DateString, OPTIONAL $_SrcFormat, $_NoTime)
 
  Dim $_, $_C						; temp var, char 
  Dim $_Date, $_Time					; date and time values 
  Dim $_aTmp1, $_aTmp2					; working arrays 
  
  If Not InStr($_DateString, ':')
    $_DateString = $_DateString + ' 00:00:00'           ; no time specified, use midnight 
  EndIf
 
  $_DateString = Replace($_DateString, ' AM', ':AM')	; make AM/PM part of the time array 
  $_DateString = Replace($_DateString, ' PM', ':PM')
 
  $_aTmp1 = Split(Trim($_DateString), ' ')		; split into date/time parts 
 
  If UBound($_aTmp1) <> 1				; must have just date and time values 
    $TimeFix = 'Invalid Source Format!'
    Exit 87
  EndIf
 
  If InStr($_aTmp1[0], ':')				; TIME is first value 
    $_Date = $_aTmp1[1]
    $_Time = $_aTmp1[0]
  Else							; DATE is first value 
    $_Date = $_aTmp1[0]
    $_Time = $_aTmp1[1]
  EndIf
 
  ; Process the Time string, returning HH:MM:SS format 
  $_aTmp2 = Split($_Time, ':')
  If UBound($_aTmp2) = 3				; have AM/PM value 
    If $_aTmp2[3] = 'PM' And $_aTmp2[0] <> '12'		; Convert PM values to 24-hour 
      $_aTmp2[0] = 12 + $_aTmp2[0]
    EndIf
    If $_aTmp2[3] = 'AM' and $_aTmp2[0] = '12'		; convert 12am to "00" 
      $_aTmp2[0] = '00'
    EndIf
  EndIf
  ; pad with zeros 
  For $_ = 0 to 2
    $_aTmp2[$_] = Right('00' + $_aTmp2[$_], 2)
  Next
  Redim Preserve $_aTmp2[2]
  $_Time = Join($_aTmp2, ':')
 
 
  ; Process the Date string, returning YYYY/MM/DD 
  Select
   Case InStr($_Date, '/')				; "/" delimited 
    $_aTmp2 = Split('X/' + $_Date, '/')
   Case InStr($_Date, '-')				; "-" delimited 
    $_aTmp2 = Split('X-' + $_Date, '-')
   Case 1
    $TimeFix = 'Invalid Date Format!'
    Exit 87
  EndSelect
 
  ; attempt to determine source date format if not specified in args 
  If Not $_SrcFormat
    If Len($_aTmp2[1]) = 4 $_SrcFormat = 'YMD' EndIf	; in standard format 
    If Len($_aTmp2[3]) = 4 $_SrcFormat = 'MDY' EndIf	; broad assumption but common! 
  EndIf
 
  ; now have date in array. Use the $_SrcFormat string to map Y, M, and D 
  Redim Preserve $_aTmp1[2]
  For $_ = 1 To 3
    $_C = SubStr($_SrcFormat, $_, 1)
    Select
     Case $_C = 'Y'
      $_aTmp1[0] = $_aTmp2[$_]
 
     Case $_C = 'M'
      $_aTmp1[1] = Right('00' + $_aTmp2[$_], 2)
 
     Case $_C = 'D'
      $_aTmp1[2] = Right('00' + $_aTmp2[$_], 2)
    EndSelect
  Next
  $_Date = Join($_aTmp1, '/')
 
  $TimeFix = $_Date
 
  If Not $_NoTime
    $TimeFix = $TimeFix + ' ' + $_Time
  EndIf
 
  Exit 0
 
EndFunction