- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateFormatCustom.vbs
More file actions
Latest commit
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.
'--------------------------------------------------------------------------------
FunctionFormatCustomDate(vInputDate,vSeparator)
DimvDatePart,vDelimiter,vParts
FormatCustomDate=""
vDatePart=Trim(vInputDate&"")
IfvDatePart=""ThenExitFunction
' drop the optional "h:m:s" tail
IfInStr(vDatePart," ")>0ThenvDatePart=Left(vDatePart,InStr(vDatePart," ")-1)
vDelimiter=DetectDelimiter(vDatePart)
IfvDelimiter=""ThenExitFunction
vParts=Split(vDatePart,vDelimiter)
IfUBound(vParts)<>2ThenExitFunction
FormatCustomDate=FourDigitYear(vParts(2))&vSeparator&_
PadTwo(vParts(0))&vSeparator&PadTwo(vParts(1))
EndFunction
'--------------------------------------------------------------------------------
' Function List
'--------------------------------------------------------------------------------
FunctionDetectDelimiter(vValue)
DimvCandidate
DetectDelimiter=""
ForEachvCandidateInArray("/","-",".")
IfInStr(vValue,vCandidate)>0Then
DetectDelimiter=vCandidate
ExitFunction
EndIf
Next
EndFunction
FunctionPadTwo(vValue)
PadTwo=Right("0"&Trim(vValue),2)
EndFunction
' Two digit years are read as 20xx: this data comes from SAP exports, which have
' no dates before 2000 in this scenario.
FunctionFourDigitYear(vValue)
DimvYear
vYear=Trim(vValue)
IfLen(vYear)=2ThenvYear="20"&vYear
FourDigitYear=vYear
EndFunction