-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateFormatCustom.vbs
More file actions
68 lines (54 loc) · 2.28 KB
/
Copy pathDateFormatCustom.vbs
File metadata and controls
68 lines (54 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Option Explicit
'--------------------------------------------------------------------------------
' ScriptName : DateFormatCustom.vbs (was DateFormateCustom.vbs)
' Creator : Sungman Han
' Creation Date : 2019-06-21
' Description : Normalises a date written as m/d/y (also m-d-y and m.d.y, with an
' optional " h:m:s" tail) into y<sep>m<sep>d with a zero padded
' month and day.
'
' Usage : FormatCustomDate("6/21/2019 13:05:00", "") -> "20190621"
' FormatCustomDate("6-21-19", "-") -> "2019-06-21"
' FormatCustomDate("garbage", "-") -> "" (never raises)
'
' Note : SAP date fields are usually written as yyyyMMdd, which is why the
' separator is a parameter and an empty separator is the common case.
'--------------------------------------------------------------------------------
Function FormatCustomDate(vInputDate, vSeparator)
Dim vDatePart, vDelimiter, vParts
FormatCustomDate = ""
vDatePart = Trim(vInputDate & "")
If vDatePart = "" Then Exit Function
' drop the optional "h:m:s" tail
If InStr(vDatePart, " ") > 0 Then vDatePart = Left(vDatePart, InStr(vDatePart, " ") - 1)
vDelimiter = DetectDelimiter(vDatePart)
If vDelimiter = "" Then Exit Function
vParts = Split(vDatePart, vDelimiter)
If UBound(vParts) <> 2 Then Exit Function
FormatCustomDate = FourDigitYear(vParts(2)) & vSeparator & _
PadTwo(vParts(0)) & vSeparator & PadTwo(vParts(1))
End Function
'--------------------------------------------------------------------------------
' Function List
'--------------------------------------------------------------------------------
Function DetectDelimiter(vValue)
Dim vCandidate
DetectDelimiter = ""
For Each vCandidate In Array("/", "-", ".")
If InStr(vValue, vCandidate) > 0 Then
DetectDelimiter = vCandidate
Exit Function
End If
Next
End Function
Function PadTwo(vValue)
PadTwo = Right("0" & Trim(vValue), 2)
End Function
' Two digit years are read as 20xx: this data comes from SAP exports, which have
' no dates before 2000 in this scenario.
Function FourDigitYear(vValue)
Dim vYear
vYear = Trim(vValue)
If Len(vYear) = 2 Then vYear = "20" & vYear
FourDigitYear = vYear
End Function