Is there a simple command for formatting a date to the UTC date format
(yy/mm/dd) in VBscript?
I could only find this function: FormatDateTime(Date[,NamedFormat])
But this function will not format the date to yy/mm/dd.
Any suggestions on how to format the date to "yy/mm/dd"?
Question 2
What is the easiest way to calculate the time offset (my web server is in
America and I want to have Australian time)
To format the date I use session.LCID=3081
Is there anything that I could set to the Australian time offset so when I
call the time() function it would calculate the offset automatically?
At the moment I use the dateadd ("n", -10, now()) function
Regards
Gonzal
Just found a solution to this problem (from this group, 3 years ago).
Works great under Win2K Pro.
'NOTE: This Requires ADO and the MSSTDFMT.dll
' If not available, use FormatNumber(),
' FormatCurrency(), or FormatDate().
Function Format(vExpression, sFormat)
'By Scott Dixon
'http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=4a5f01c0b58a%24bfa834c0%249ee62ecf%40tkmsftngxa05&rnum=6&prev=/groups%3Fq%3DFormat%2Bfunction%2Bgroup:microsoft.public.scripting.vbscript%2Bgroup:microsoft.public.scripting.vbscript%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dmicrosoft.public.scripting.vbscript
Const adVariant = 12
Dim objFMT
Dim objRS
If Len(vExpression) > 0 Then
Set objFMT = CreateObject("MSSTDFMT.StdDataFormat")
objFMT.Format = sFormat
Set objRS = CreateObject("ADODB.Recordset")
objRS.Fields.Append "fldExpression", adVariant
objRS.Open
objRS.AddNew
'Apply format
Set objRS("fldExpression").DataFormat = objFMT
objRS("fldExpression").Value = vExpression
Format = objRS("fldExpression").Value
Else
Format = ""
End If
Set objRS = Nothing
Set objFMT = Nothing
End Function
>Is there a simple command for formatting a date to the UTC date format
>(yy/mm/dd) in VBscript?
Misnomer. There is no UTC date format. What you have is (close to) the
ISO date format, yyyy-mm-dd.
>Any suggestions on how to format the date to "yy/mm/dd"?
Format it to any *fixed* format using yy or yyyy, mm, & dd, then re-
format with a RegExp replace if available or by string manipulation. Or
build it from the numbers for year, month, day, remembering to add
leading zeroes.
>What is the easiest way to calculate the time offset (my web server is in
>America and I want to have Australian time)
>
>To format the date I use session.LCID=3081
>
>Is there anything that I could set to the Australian time offset so when I
>call the time() function it would calculate the offset automatically?
You will probably need to determine the Australian rules and implement
them yourself. You should know that Australia has three major Time
Zones (and more for the associated islands), and that some parts have
Summer Time while others do not.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MSIE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Dr John Stockton
You wrote:
> Or
> build it from the numbers for year, month, day, remembering to add
> leading zeroes.
Do you mean: When I have a date 31/01/2003 and I convert the date to
2003/1/31 I should add a leading zero to the month? So it will look like
this. 2003/01/31
Is there a particular reason way it should be done like this?
Anyway I have end up with this code:
Dim thisdate, thismonth, thisday
thisdate = dateadd("n",application("localoffset"),now())
if len(month(thisdate)) = 1 then thismonth = "0"& month(thisdate) else
thismonth = month(thisdate) end if
if len(day(thisdate)) = 1 then thisday = "0"& day(thisdate) else thisday =
day(thisdate) end if
thisdate = year(thisdate)& "/"& thismonth& "/"& thisday
Regards
"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote in message
news:rl6m1EEd...@merlyn.demon.co.uk...
>You wrote:
>> Or
>> build it from the numbers for year, month, day, remembering to add
>> leading zeroes.
>
>Do you mean: When I have a date 31/01/2003 and I convert the date to
>2003/1/31 I should add a leading zero to the month? So it will look like
>this. 2003/01/31
Yes.
>Is there a particular reason way it should be done like this?
International standard; possibly AU standard; fixed-length; can be
sorted as strings. See via sig below.
>Anyway I have end up with this code:
>
>Dim thisdate, thismonth, thisday
>thisdate = dateadd("n",application("localoffset"),now())
>if len(month(thisdate)) = 1 then thismonth = "0"& month(thisdate) else
>thismonth = month(thisdate) end if
>if len(day(thisdate)) = 1 then thisday = "0"& day(thisdate) else thisday =
>day(thisdate) end if
>thisdate = year(thisdate)& "/"& thismonth& "/"& thisday
Neater to use a function for adding leading zero.
The following works for me, though it can probably be improved :
<script type="text/vbscript">
function ldgz(ByVal N) ' N presumed integer
if (N>=0) and (N<10) then ldgz= "0"+Cstr(N) else ldgz=Cstr(N)
end function
Dim Yr, Mo, Dy, Hr, Mi, Sc
Yr = Year(Now) : Mo = ldgz(Month(Now)) : Dy = ldgz(Day(Now))
Hr = ldgz(Hour(Now)) : Mi = ldgz(Minute(Now)) : Sc = ldgz(Second(Now))
document.write Yr & "-" & Mo & "-" & Dy, "T", Hr & ":" & Mi & ":" & Sc
</script>
Indeed, none of the variables in the Dim is really needed;
document.write Year(Now) & "-" & ldgz(Month(Now)) & "-" & ldgz(Day(Now))
--
© 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.
slightly shorter version is this (it really doesn't matter how whether the
day or month starts being one digit, as long as it ends as two!)
Dim thisdate
thisdate = dateadd("n",application("localoffset"),now())
thisdate = year(thisdate)& "/"& right("0" & month(thisdate),2) & _
"/"& right("0" & day(thisdate),2)
(just a different view of things!)
--
Rickety