hi,
I'd like to format a date using vbscript to this format:
1995-01-03 00:00:00

Unfortunately, if I use month(date), then it returns me a single digit
number for January through September. Same goes for the day(date) function.
Is there an easy way to retrieve this date format? I know .net cdate can do
this..
cdate(strdate).tostring("s") to get that format. Is there an equivalent in
vbscript?

Thanks,
Vicky

Re: formatting dates by McKirahan

McKirahan
Mon Sep 13 12:39:09 CDT 2004

"Rainstorms21" <rainstorms21@--nospam--comcast.net> wrote in message
news:ep7rbObmEHA.412@TK2MSFTNGP10.phx.gbl...
>
> hi,
> I'd like to format a date using vbscript to this format:
> 1995-01-03 00:00:00
>
> Unfortunately, if I use month(date), then it returns me a single digit
> number for January through September. Same goes for the day(date)
function.
> Is there an easy way to retrieve this date format? I know .net cdate can
do
> this..
> cdate(strdate).tostring("s") to get that format. Is there an equivalent in
> vbscript?
>
> Thanks,
> Vicky


Here's one solution; watch for word-wrap.

Option Explicit
Dim strNOW
strNOW = Now()
Dim strYMD
strYMD = DatePart("yyyy",strNOW) & "-"
strYMD = strYMD & Right("0" & CStr(DatePart("m",strNOW)),2) & "-"
strYMD = strYMD & Right("0" & CStr(DatePart("d",strNOW)),2) & " "
strYMD = strYMD & Right("0" & CStr(DatePart("h",strNOW)),2) & ":"
strYMD = strYMD & Right("0" & CStr(DatePart("n",strNOW)),2) & ":"
strYMD = strYMD &Right("0" & CStr( DatePart("s",strNOW)),2)
WScript.Echo strYMD



Re: formatting dates by Rainstorms21

Rainstorms21
Mon Sep 13 14:22:05 CDT 2004


Thanks!

"McKirahan" <News@McKirahan.com> wrote in message
news:1Xk1d.186936$mD.57915@attbi_s02...
> "Rainstorms21" <rainstorms21@--nospam--comcast.net> wrote in message
> news:ep7rbObmEHA.412@TK2MSFTNGP10.phx.gbl...
>>
>> hi,
>> I'd like to format a date using vbscript to this format:
>> 1995-01-03 00:00:00
>>
>> Unfortunately, if I use month(date), then it returns me a single digit
>> number for January through September. Same goes for the day(date)
> function.
>> Is there an easy way to retrieve this date format? I know .net cdate can
> do
>> this..
>> cdate(strdate).tostring("s") to get that format. Is there an equivalent
>> in
>> vbscript?
>>
>> Thanks,
>> Vicky
>
>
> Here's one solution; watch for word-wrap.
>
> Option Explicit
> Dim strNOW
> strNOW = Now()
> Dim strYMD
> strYMD = DatePart("yyyy",strNOW) & "-"
> strYMD = strYMD & Right("0" & CStr(DatePart("m",strNOW)),2) & "-"
> strYMD = strYMD & Right("0" & CStr(DatePart("d",strNOW)),2) & " "
> strYMD = strYMD & Right("0" & CStr(DatePart("h",strNOW)),2) & ":"
> strYMD = strYMD & Right("0" & CStr(DatePart("n",strNOW)),2) & ":"
> strYMD = strYMD &Right("0" & CStr( DatePart("s",strNOW)),2)
> WScript.Echo strYMD
>
>



Re: formatting dates by Dr

Dr
Wed Sep 15 06:13:32 CDT 2004

JRS: In article <1Xk1d.186936$mD.57915@attbi_s02>, dated Mon, 13 Sep
2004 17:39:09, seen in news:microsoft.public.scripting.vbscript,
McKirahan <News@McKirahan.com> posted :
>"Rainstorms21" <rainstorms21@--nospam--comcast.net> wrote in message
>news:ep7rbObmEHA.412@TK2MSFTNGP10.phx.gbl...

>> I'd like to format a date using vbscript to this format:
>> 1995-01-03 00:00:00

1995-01-23 would be a better example; admittedly, I've never seen Y D M
in use. Also 13:14:15, to make the 24-hour clock blatantly obvious.


>> Unfortunately, if I use month(date), then it returns me a single digit
>> number for January through September. Same goes for the day(date)
>function.
>> Is there an easy way to retrieve this date format? I know .net cdate can
>do
>> this..
>> cdate(strdate).tostring("s") to get that format. Is there an equivalent in
>> vbscript?

McK : don't over-quote; don't quote signatures.


>Here's one solution; watch for word-wrap.

Preventing wrap is an author's responsibility.

>Option Explicit
>Dim strNOW
> strNOW = Now()
>Dim strYMD
> strYMD = DatePart("yyyy",strNOW) & "-"
> strYMD = strYMD & Right("0" & CStr(DatePart("m",strNOW)),2) & "-"
> strYMD = strYMD & Right("0" & CStr(DatePart("d",strNOW)),2) & " "
> strYMD = strYMD & Right("0" & CStr(DatePart("h",strNOW)),2) & ":"
> strYMD = strYMD & Right("0" & CStr(DatePart("n",strNOW)),2) & ":"
> strYMD = strYMD &Right("0" & CStr( DatePart("s",strNOW)),2)
>WScript.Echo strYMD

Long-winded, as is typical. Note : strNOW is not a string, but a CDate.


Concatenating is more work than addition. Therefore, Right(100+X, 2)
beats Right("0" & X, 2).

Rather than repeating code, one should use a function.


function LZ(X) : LZ = Right(100+X, 2) : end function

D = CDate(66666.789)

Ans = Year(D) & "-" & LZ(Month(D)) & "-" & LZ(Day(D)) & " " & _
LZ(Hour(D)) & ":" & LZ(Minute(D)) & ":" & LZ(Second(D))

document.write Ans ' 2082-07-09 18:56:10


See also in <URL:http://www.merlyn.demon.co.uk/programs/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: formatting dates by Ross

Ross
Sat Sep 25 20:39:45 CDT 2004

Dr John Stockton <spam@merlyn.demon.co.uk> wrote in
news:q7vGiVBcPCSBFwpf@merlyn.demon.co.uk:

> Concatenating is more work than addition. Therefore, Right(100+X, 2)
> beats Right("0" & X, 2).

But is it more work than addition plus implicit conversion?

Re: formatting dates by Dr

Dr
Sun Sep 26 16:19:11 CDT 2004

JRS: In article <Xns956FDCAAE96E1pt101594@129.250.170.82>, dated Sun,
26 Sep 2004 01:39:45, seen in news:microsoft.public.scripting.vbscript,
Ross Presser <rpresser@NOSPAM.imtek.com.invalid> posted :
>Dr John Stockton <spam@merlyn.demon.co.uk> wrote in
>news:q7vGiVBcPCSBFwpf@merlyn.demon.co.uk:
>
>> Concatenating is more work than addition. Therefore, Right(100+X, 2)
>> beats Right("0" & X, 2).
>
>But is it more work than addition plus implicit conversion?

In this case, X is a number known to be an integer within 00..99; each
method has one implicit conversion of integer to string.

On my system, the difference in speed is not great, but it seems
definite. Did you find differently on yours?

T0 = Timer
for K = 0 to 9999
for X = 0 to 99
z = Right(100+X, 2)
'z = Right("0" & X, 2)
next
next

document.write "OK ", Timer-T0

Comment each z-assignment in turn, and both to assess overhead.

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