'********************************************************************
'*
'* Function DateTimeFormat
'*
'* Author: NetworkAdminKB.com
'* Created: 2006-01-11
'* Modified: 2006-01-11
'*
'* Purpose: Returns the given Date/Time as a string in following format
'* YYYY-MM-DD HH:MM:SS (24h ISO 8601 Format).
'*
'* Input:
'* dtmDateValue A string or Date/Time value in one of the following formats.
'* "1/10/2006 7:45:41 PM"
'* "1/10/2006"
'* "7:45:41 PM"
'*
'* Notes: If only the date or time is passed in, then the corresponding
'* result is also only the date or time.
'*
'* Output: Returns the given Date as a string in the requested format.
'*
'* Calls:
'* TwoDigit
'* Time24Hour
'*
'********************************************************************
Function DateTimeFormat(ByVal dtmDateValue)
'Version: 1.0 2006-01-11
Dim aryParts, aryDate, strDate, strNewDate, strTime
Dim strJoin
strDate = CStr(dtmDateValue)
strJoin = "-"
'Year/Month/Date 24Time
'4 digit year, 2 digit month, 2 digit day
aryParts = Split(strDate, " ")
aryDate = Split(aryParts(0),"/")
If UBound(aryDate) = 2 Then
Select Case UBound(aryParts)
Case 0
Case 1
Case 2
strTime = aryParts(1) & " " & aryParts(2)
strTime = Time24Hour(strTime)
End Select 'UBound(aryParts)
If IsEmpty(strTime) Then
strNewDate = aryDate(2) & strJoin & TwoDigit(aryDate(0)) & strJoin & _
TwoDigit(aryDate(1))
Else
strNewDate = aryDate(2) & strJoin & TwoDigit(aryDate(0)) & strJoin & _
TwoDigit(aryDate(1)) & " " & strTime
End If 'strTime
Else
'Only the 12 hour time was passed, convert to 24 hour and return
strNewDate = Time24Hour(dtmDateValue)
End If 'UBound(aryDate) = 2
DateTimeFormat = strNewDate
End Function 'DateTimeFormat
'********************************************************************
'*
'* Function DateTimeFormat
'*
'* Author: NetworkAdminKB.com
'* Created: 2006-01-11
'* Modified: 2006-05-16
'*
'* Purpose: Returns the given Date/Time as a string in the requested format.
'*
'* Input:
'* dtmDateValue A string or Date/Time value in one of the following formats.
'* "1/10/2006 7:45:41 PM"
'* "1/10/2006"
'* "7:45:41 PM"
'* intFormat A constant indicating which format to return
'* the Date/Time in.
'* 1 = YYYY/MM/DD HH:MM:SS ?M (12h Time Format)
'* 2 = YYYY/MM/DD HH:MM:SS (24h Time Format, Default)
'* 3 = YYYY-MM-DD HH:MM:SS ?M (12h Time Format)
'* 4 = YYYY-MM-DD HH:MM:SS (24h ISO 8601 Format)
'*
'* Notes: If only the date or time is passed in, then the corresponding
'* result is also only the date or time.
'* 12h Time is formated as two digits per number (07:45:41 PM)
'* 24h Time is formated as two digits per number (07:45:41)
'*
'* Output: Returns the given Date as a string in the requested format.
'*
'* Calls:
'* TwoDigit
'* Time24Hour
'* TimeFormat
'*
'* Changes:
'* 2006-05-16: Added support for more date and time formats.
'********************************************************************
Function DateTimeFormat(ByVal dtmDateValue, ByVal intFormat)
'Version: 1.5 2006-05-16
Dim aryParts, aryDate, strDate, strNewDate, strTime
Dim strJoin
strDate = CStr(dtmDateValue)
If intFormat > 4 Or intFormat < 1 Then
intFormat = 2
End If 'intFormat > 4 Or intFormat < 1
'Set the Date Format Join Char.
Select Case intFormat
Case 1,2
strJoin = "/"
Case 3,4
strJoin = "-"
End Select 'intFormat
Select Case intFormat
Case 1,3
'Year/Month/Day 12Time
'4 digit year, 2 digit month, 2 digit day
aryParts = Split(strDate, " ")
aryDate = Split(aryParts(0),"/")
If UBound(aryDate) = 2 Then
Select Case UBound(aryParts)
Case 0
Case 1
Case 2
strTime = aryParts(1) & " " & aryParts(2)
strTime = TimeFormat(strTime)
End Select 'UBound(aryParts)
If IsEmpty(strTime) Then
strNewDate = aryDate(2) & strJoin & TwoDigit(aryDate(0)) & strJoin & _
TwoDigit(aryDate(1))
Else
strNewDate = aryDate(2) & strJoin & TwoDigit(aryDate(0)) & strJoin & _
TwoDigit(aryDate(1)) & " " & strTime
End If 'strTime
Else
'Only the 12 Hour time was passed, return the properly formated value.
strNewDate = TimeFormat(dtmDateValue)
End If 'UBound(aryDate) = 2
Case 2,4
'Year/Month/Date 24Time
'4 digit year, 2 digit month, 2 digit day
aryParts = Split(strDate, " ")
aryDate = Split(aryParts(0),"/")
If UBound(aryDate) = 2 Then
Select Case UBound(aryParts)
Case 0
Case 1
Case 2
strTime = aryParts(1) & " " & aryParts(2)
strTime = Time24Hour(strTime)
End Select 'UBound(aryParts)
If IsEmpty(strTime) Then
strNewDate = aryDate(2) & strJoin & TwoDigit(aryDate(0)) & strJoin & _
TwoDigit(aryDate(1))
Else
strNewDate = aryDate(2) & strJoin & TwoDigit(aryDate(0)) & strJoin & _
TwoDigit(aryDate(1)) & " " & strTime
End If 'strTime
Else
'Only the 12 hour time was passed, convert to 24 hour and return
strNewDate = Time24Hour(dtmDateValue)
End If 'UBound(aryDate) = 2
Case Else
End Select 'intFormat
DateTimeFormat = strNewDate
End Function 'DateTimeFormat
Article ID: 382, Created On: 9/25/2011, Modified: 9/25/2011