Dr
Mon Feb 27 14:20:30 CST 2006
JRS: In article <uuHYOm3OGHA.1032@TK2MSFTNGP11.phx.gbl>, dated Mon, 27
Feb 2006 09:41:08 remote, seen in news:microsoft.public.scripting.vbscri
pt, Alexander Mueller <millerax@hotmail.com> posted :
>Dr John Stockton schrieb:
>> JRS: In article <43ff9412$0$508$9b4e6d93@newsread4.arcor-online.net>,
>> dated Sat, 25 Feb 2006 00:17:31 remote, seen in news:microsoft.public.sc
>> ripting.vbscript, Alexander Mueller <millerax@hotmail.com> posted :
>>>
>>>
>>> Of course you could refine the pattern for also approximatively
>>> validating the input string to match YYYYMMDD-format.
>>>
>>> r.Pattern = "^(19\d\d|20\d\d)(0[1-9]|1[0-2])([0-2]\d|3[01])$"
>>>
>>
>> That would be rather silly, though, as it is so much nicer to use RegExp
>> Match (in javascript; I suppose VBS can do likewise) to verify and split
>> YYYYMMDD (or YYYY-MM-DD) into three digit-string fields YYYY MM DD, then
>> put those into (JS) new Date(,,) or (VB) DateSerial(,,) and test the
>> result for a match of the M & D values.
>
>lol, it's not at all smart and simply kind of clumsy to manually extract
>three strings using Mid, Left and friends,
But I did not say to use Mid/Left/Right; I said to use RegExp. Read
what you quoted.
> then convert them to date,
>which may fail in case of being not numeric,
It will not fail, because the RegExp test has ensured that they are
numeric (and, if VBS RegExps have the capability of JS ones, has also
separated the parts).
> then compare each part with
>the extracted part to check if the date parts from the source string
>were valid and then finally do the format to iso-issue.
>
>RegExp does five steps in one:
>check strings length,
>check pattern to be numeric
>check dateparts to match bounds for datepart
>optionally validate date-range
>format/convert date to iso
Your RegExp allows Apr 31, Jun 31, Sep 31, Nov 31, Feb 31, Feb 30, and
Feb 29 in any year. That's inferior.
>Your solution:
No : your feeble prediction.
>
>Dim s, dateIso, dateVal
>Dim m, d, y
>
>s = "20060223"
>If Len(s)<> 8 Then
Not needed after RegExp check for /^(\d\d\d\d)(\d\d)(\d\d)$/
> MsgBox "String-Lenght invalid", 16
> WSH.Quit
>End if
>y = Left(s, 4)
>m = Mid(s, 5, 2)
>d = Right(s, 2)
Should not be needed if VBS RegExp can give the parts.
>On Error Resume Next
>dateVal = DateSerial(y, m, d)
>If Err Then
Not needed; all numeric YYYY MM DD are acceptable to DateSerial.
> MsgBox "Source is not YYYYMMDD", 16
> WSH.Quit
>End if
>On Error Goto 0
>
>If Year(dateVal)<> CInt(y) Or _
> Month(dateVal)<> CInt(m) Or _
> Day(dateVal)<> CInt(d) Then
> MsgBox "Source is not YYYYMMDD", 16
> WSH.Quit
>End If
It is only necessary to do two of those tests.
>dateIso = Year(dateVal) & "/" _
> & Right("0" & Month(dateVal), 2) & "/" _
> & Right("0" & Day(dateVal), 2)
>
>MsgBox s & " -> " & dateIso
Right(100 + Month(dateVal), 2) is somewhat better than
Right("0" & Month(dateVal), 2) , IIRC.
>My Solution:
>
>Dim s, dateIso
>Dim r
>Set r = New regExp
>r.Pattern = "^(19\d\d|20\d\d)(0[1-9]|1[0-2])([0-2]\d|3[01])$"
>
>s = "20060223"
>if r.test(s) Then
> dateIso = r.Replace(s, "$3/$2/$1")
> MsgBox s & " -> " & dateIso
>else
> MsgBox "input-string """ & s & """ doesn't match YYYYMMDD"
>end if
You've not properly validated the date. The transformation is as I
indicated in my first post, and already translated by you.
Note that your year-handling will not accept the birth-dates of some who
are still living; nor (we hope) the death-dates of some already born.
Limited-range date code should be used only when necessary or known
safe.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:
http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:
http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.