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.