Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get date and time and format to DDMMYY-HHMM

474 views
Skip to first unread message

Sheel

unread,
Nov 28, 2007, 2:50:01 PM11/28/07
to
I am looking to get the date and time and format it into the format of
DDMMYY-HHMM so that it can be added into the creation of a folder name. Does
anyone have any ideas as to how this can be accomplished?

Thanks,
Sheel

ekkehard.horner

unread,
Nov 28, 2007, 3:38:43 PM11/28/07
to
Sheel schrieb:
Here are three different methods to do this; all are easy to adapt,
if you decide to switch to YYYYMMDD-HHMM:

Dim dtNow : dtNow = Now
Dim sRes

WScript.Echo 0, dtNow

sRes = Right( "0" & Day( dtNow ), 2 ) _
& Right( "0" & Month( dtNow ), 2 ) _
& Right( Year( dtNow ), 2 ) _
& "-" _
& Right( "0" & Hour( dtNow ), 2 ) _
& Right( "0" & Minute( dtNow ), 2 )
WScript.Echo 1, sRes

sRes = DNIPrintF( "{0:ddMMyy-HHmm}", Array( dtNow ) )
WScript.Echo 2, sRes

Dim oRE : Set oRE = New RegExp
oRE.Pattern = "^(\d\d).(\d\d).\d\d(\d\d) (\d\d):(\d\d):\d\d$"
WScript.Echo 3, oRE.Replace( CStr( dtNow ), "$1$2$3-$4$5" ) ' "$2$1.." for us

' ----------------------------------------------------------------------------
'' DNIPrintF( sFmt, aItems ) - use .NET to sprintf()
' ----------------------------------------------------------------------------
Function DNIPrintF( sFmt, aItems )
Dim oSB : Set oSB = CreateObject( "System.Text.StringBuilder" )
oSB.AppendFormat_4 sFmt, (aItems)
DNIPrintF = oSB.ToString()
End Function

output:

=== fmtDate: format date ========
0 28.11.2007 21:36:43
1 281107-2136
2 281107-2136
3 281107-2136
=== fmtDate: 0 done (00:00:00) ==

Dr J R Stockton

unread,
Nov 29, 2007, 8:00:37 AM11/29/07
to
In microsoft.public.scripting.vbscript message <474dd1d8$0$16665$9b4e6d9
3...@newsspool3.arcor-online.net>, Wed, 28 Nov 2007 21:38:43,
ekkehard.horner <ekkehar...@arcor.de> posted:


> sRes = Right( "0" & Day( dtNow ), 2 ) _
> & Right( "0" & Month( dtNow ), 2 ) _
> & Right( Year( dtNow ), 2 ) _
> & "-" _
> & Right( "0" & Hour( dtNow ), 2 ) _
> & Right( "0" & Minute( dtNow ), 2 )

sRes = Year(dtNow)*1e4 + Month(dtNow)*1e2 + Day(dtNow ) _
& "-" & Right(1e4 + Hour(dtNow)*1e2 + Minute(dtNow), 4)

is better.


> Dim oRE : Set oRE = New RegExp
> oRE.Pattern = "^(\d\d).(\d\d).\d\d(\d\d) (\d\d):(\d\d):\d\d$"
> WScript.Echo 3, oRE.Replace( CStr( dtNow ), "$1$2$3-$4$5" ) '
>"$2$1.." for us

Any method based in CStr of a CDate is unreliable, unless there is
absolute control over the default date field order. Some Europeans use
DMY, some choose YMD.


Query : given such a RegExp, exactly how best can one, in (Web) VBS,
obtain, in separate variables or in an array, the values corresponding
to $1 .. $5 ?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
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.

ekkehard.horner

unread,
Nov 30, 2007, 5:34:16 AM11/30/07
to
Dr J R Stockton schrieb:

> In microsoft.public.scripting.vbscript message <474dd1d8$0$16665$9b4e6d9
> 3...@newsspool3.arcor-online.net>, Wed, 28 Nov 2007 21:38:43,
> ekkehard.horner <ekkehar...@arcor.de> posted:
>
>
>> sRes = Right( "0" & Day( dtNow ), 2 ) _
>> & Right( "0" & Month( dtNow ), 2 ) _
>> & Right( Year( dtNow ), 2 ) _
>> & "-" _
>> & Right( "0" & Hour( dtNow ), 2 ) _
>> & Right( "0" & Minute( dtNow ), 2 )
>
> sRes = Year(dtNow)*1e4 + Month(dtNow)*1e2 + Day(dtNow ) _
> & "-" & Right(1e4 + Hour(dtNow)*1e2 + Minute(dtNow), 4)
>
> is better.
as "good" depends on who wants to do what, you are as right for
some conditions as you are wrong for others.

>
>
>> Dim oRE : Set oRE = New RegExp
>> oRE.Pattern = "^(\d\d).(\d\d).\d\d(\d\d) (\d\d):(\d\d):\d\d$"
>> WScript.Echo 3, oRE.Replace( CStr( dtNow ), "$1$2$3-$4$5" ) '
>> "$2$1.." for us
>
> Any method based in CStr of a CDate is unreliable, unless there is
> absolute control over the default date field order. Some Europeans use
> DMY, some choose YMD.
>
thanks for emphasizing this important aspect

>
> Query : given such a RegExp, exactly how best can one, in (Web) VBS,
> obtain, in separate variables or in an array, the values corresponding
> to $1 .. $5 ?
use oRE.Execute( <Text> )( <NumberOfMatch> ).SubMatches( <NumberOfParen> )

Dr J R Stockton

unread,
Nov 30, 2007, 4:25:22 PM11/30/07
to
In microsoft.public.scripting.vbscript message <474fe72c$0$13112$9b4e6d9
3...@newsspool2.arcor-online.net>, Fri, 30 Nov 2007 11:34:16,
ekkehard.horner <ekkehar...@arcor.de> posted:

>> Query : given such a RegExp, exactly how best can one, in (Web) VBS,
>> obtain, in separate variables or in an array, the values corresponding
>> to $1 .. $5 ?
>use oRE.Execute( <Text> )( <NumberOfMatch> ).SubMatches( <NumberOfParen> )

Thanks. SubMatches is the clue. I've not yet found out how I should
check what Execute returns for no matches, but can work round using "for
each X in ...". Code is, temporarily, in <URL:http://www.merlyn.demon.
co.uk/vb-date3.htm>.

Since the present RegExp pattern starts ^ and ends $, the number of
matches will be 0 or 1.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
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 vb-dates.htm pas-time.htm critdate.htm etc.

0 new messages