Adding dates to an XML Node

N

news

This had me stumped for ages yesterday, and since I solved it and can't
find much reference to the problem, I thought I'd post here.

I have various vbscript ASP pages that take an ADO recordset and dump
it out as XML into the page so that clientside processes can use it and
a number of XSLT stylesheets to present/filter/re-sort the data without
a trip back to the server.

Yesterday I noticed that one of the pages insisted on displaying the
dates in US MM-DD-YYYY format. I'm in the UK and had thought I'd set
all the locales correctly to use UK dates.

Turns out I had, except for the following situation:

If you take a variable that has a date in it and assign it directly to
the .text attibute of a DOMNode object, it is converted to text in the
US format, regardless of the system/user/whatever locales set. If you
cstr() the date first, it will be converted correctly.

set xmlDoc = server.createobject("MSXML2.DOMdocument.4.0")
set xmlNode = xmlDoc.createElement("foo")

session.LCID = 2057 'Force UK locale for all those elsewhere
datevar = #2006/1/12# '12th January

response.write("datevar = " & datevar & "<br>")
xmlNode.text = datevar
response.write("xmlNode.text = datevar, results in " & xmlNode.text &
"<br>") ' results in "01/12/2006"
xmlNode.text = cstr(datevar)
response.write("xmlNode.text = cstr(datevar), results in " &
xmlNode.text) ' results in "12/01/2006"

In other words, perform the string conversion within VBscript, not
within MSXML2. One for the ASPFAQ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top