I have date values in ISO formats

20060223 (today)

I should format it in common use:

23/02/2006 or 23 Feb 2006 and so on..

Can I do it natively in VBScript?

Any help appreciated.

Best regards.


--
fabri
Sei un italiano medio, di oltre trent'anni se:
Sai che il codice SYS64738 serviva per riavviare il Commodore 64 ed hai
cambiato almeno una mezza dozzina di joystick Quickshot I giocando a
Summer Games.
MKDS nick & friend Code:
Joker® - 055895 043343

RE: Is it possible to convert ISO date format? by jefrie

jefrie
Thu Feb 23 11:20:27 CST 2006

Hello Fabri,

i did not find anything appropriate ...
Maybe you can use this code in a selfmade function:

[code]
strDate = "20060223"
MyDate1 = DateSerial(Mid(strDate,1,4),Mid(strDate,5,2),Mid(strDate,7,2))
WScript.Echo "Test: " & MyDate1
[/code]
Now, you have a date type and you can output it in (nearly) any form.

<pre>

FormatDateTime(Date[, NamedFormat])


Arguments
Date
Required. Date expression to be formatted.

NamedFormat
Optional. Numeric value that indicates the date/time format used. If
omitted, vbGeneralDate is used.

Settings
The NamedFormat argument has the following settings:

Constant Value Description
vbGeneralDate
0
Display a date and/or time. If there is a date part, display it as a short
date. If there is a time part, display it as a long time. If present, both
parts are displayed.

vbLongDate
1
Display a date using the long date format specified in your computer's
regional settings.

vbShortDate
2
Display a date using the short date format specified in your computer's
regional settings.

vbLongTime
3
Display a time using the time format specified in your computer's regional
settings.

vbShortTime
4
Display a time using the 24-hour format (hh:mm).

</pre>

--
Jens Frieben (Germany)


"Fabri" wrote:

> I have date values in ISO formats
>
> 20060223 (today)
>
> I should format it in common use:
>
> 23/02/2006 or 23 Feb 2006 and so on..
>
> Can I do it natively in VBScript?
>
> Any help appreciated.
>
> Best regards.
>
>
> --
> fabri
> Sei un italiano medio, di oltre trent'anni se:
> Sai che il codice SYS64738 serviva per riavviare il Commodore 64 ed hai
> cambiato almeno una mezza dozzina di joystick Quickshot I giocando a
> Summer Games.
> MKDS nick & friend Code:
> Joker® - 055895 043343
>

Re: Is it possible to convert ISO date format? by McKirahan

McKirahan
Thu Feb 23 11:51:27 CST 2006

"Fabri" <no@sp.am> wrote in message news:dtkn9c$eu7$1@nnrp.ngi.it...
> I have date values in ISO formats
>
> 20060223 (today)
>
> I should format it in common use:
>
> 23/02/2006 or 23 Feb 2006 and so on..
>
> Can I do it natively in VBScript?
>
> Any help appreciated.
>

WScript.Echo FormatDate(1,"20060223")
WScript.Echo FormatDate(2,"20060223")

Function FormatDate(what,when)
Dim y, m, d, s, t, z
y = Left(when,4)
m = Mid(when,5,2)
d = Right(when,2)
s = DateSerial(y,m,d)
If what = 1 Then
t = Split(FormatDateTime(s,1)," ")
z = t(2) & Left(t(1),3) & "," & t(3)
FormatDate = Replace(z,","," ")
ElseIf what = 2 Then
FormatDate = FormatDateTime(s,2)
End If
End Function


Your regional settings will determine if
"dd/mm/yyyy" or "mm/dd/yyyy" is
displayed when "what" is "2".


Look at FormatDateTime() for other formats.



Re: Is it possible to convert ISO date format? by Dr

Dr
Thu Feb 23 16:34:29 CST 2006

JRS: In article <VsadnSg9RfaCa2DenZ2dnUVZ_tWdnZ2d@comcast.com>, dated
Thu, 23 Feb 2006 11:51:46 remote, seen in news:microsoft.public.scriptin
g.vbscript, McKirahan <News@McKirahan.com> posted :
>"Fabri" <no@sp.am> wrote in message news:dtkn9c$eu7$1@nnrp.ngi.it...
>> I have date values in ISO formats
>>
>> 20060223 (today)
>>
>> I should format it in common use:
>>
>> 23/02/2006 or 23 Feb 2006 and so on..

>Function FormatDate(what,when)
> Dim y, m, d, s, t, z
> y = Left(when,4)
> m = Mid(when,5,2)
> d = Right(when,2)
> s = DateSerial(y,m,d)
> If what = 1 Then
> t = Split(FormatDateTime(s,1)," ")
> z = t(2) & Left(t(1),3) & "," & t(3)
> FormatDate = Replace(z,","," ")

To get a specific numeric format, there's no need for all that.

FormatDate = d & "/" & m & "/" & y // 23/02/2006

Any scheme which converts a date to a localised string and rearranges it
is liable to fail if the localisation changes.


That format can also be obtained with a RegExp; I cannot myself test
that in VBS, but in tested JS it is :-

S = "20060223"
S = S.replace(/(\d\d\d\d)(\d\d)(\d\d)/, "$3/$2/$1")
// 23/02/2006

However, while ISO's 20060223 is not obviously a date, ISO's 2006-02-23
is both standard (International, European, probably Italian, and maybe
even US Federal) and obvious - so it is that which IMHO you should use.

--
© 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.

Re: Is it possible to convert ISO date format? by Fabri

Fabri
Fri Feb 24 03:00:41 CST 2006

Dr John Stockton wrote:

> S = "20060223"
> S = S.replace(/(\d\d\d\d)(\d\d)(\d\d)/, "$3/$2/$1")


Please, can you translate it in vbscript?

Thx

--
fabri
Sei un italiano medio, di oltre trent'anni se:
Sai che il codice SYS64738 serviva per riavviare il Commodore 64 ed hai
cambiato almeno una mezza dozzina di joystick Quickshot I giocando a
Summer Games.
MKDS nick & friend Code:
Joker® - 055895 043343

Re: Is it possible to convert ISO date format? by Dr

Dr
Fri Feb 24 16:55:22 CST 2006

JRS: In article <dtmhsl$42o$1@nnrp.ngi.it>, dated Fri, 24 Feb 2006
10:00:41 remote, seen in news:microsoft.public.scripting.vbscript, Fabri
<no@sp.am> posted :
>Dr John Stockton wrote:
>
>> S = "20060223"
>> S = S.replace(/(\d\d\d\d)(\d\d)(\d\d)/, "$3/$2/$1")
>
>
>Please, can you translate it in vbscript?

No, and I indicated why before. No doubt someone else will be able to.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Re: Is it possible to convert ISO date format? by Dr

Dr
Sat Feb 25 16:42:50 CST 2006

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.

Then there's no gratuitous century limit, and the length of the month is
correctly handled; the date is fully validated.

See via below, js-dates.htm & vb-dates.htm.

--
© 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.

Re: Is it possible to convert ISO date format? by Alexander

Alexander
Mon Feb 27 02:41:08 CST 2006

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, then convert them to date,
which may fail in case of being not numeric, 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 solution:

Dim s, dateIso, dateVal
Dim m, d, y

s = "20060223"
If Len(s)<> 8 Then
MsgBox "String-Lenght invalid", 16
WSH.Quit
End if
y = Left(s, 4)
m = Mid(s, 5, 2)
d = Right(s, 2)

On Error Resume Next
dateVal = DateSerial(y, m, d)
If Err Then
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

dateIso = Year(dateVal) & "/" _
& Right("0" & Month(dateVal), 2) & "/" _
& Right("0" & Day(dateVal), 2)

MsgBox s & " -> " & dateIso


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

Mfg,
Alex


Re: Is it possible to convert ISO date format? by Dr

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.