JScript to set a Date Picker to Today

D

devers

I've been try to set a Date Picker control in JScript to the current date.
When I set the node to the results of Date() it is in an invalid format for
any of the option I could find on the date picker control. When I try to
parse out the date to send "YYYY-MM-DD" to the control I get jibberish. I
tried simply modify the sample GetDateDemo funciton that the JScript help
file shows for the GetDate funciton but that did not return the epxected
results either.

I've finally just set the rules on the control to do the work but I am not
sure that it is going to get set properly as the form is part of a workflow
and when the last person completes it I need to set the completed date.

Here's the code I tried, a lsightly modified version of thier code to give
InfoPath the format it said it neexded:

function DateDemo()
{
var d, s ;
d = new Date();
s = d.getYear();
s = s + "-" + (d.getMonth() + 1) + "-";
s = s + d.getDate();
return(s);
}

This returned 0 - the number 0.

Does anyone know how to actually set a date picker to todays date using
JScript in InfoPath?

Thanks!
 
S

S.Y.M. Wong-A-Ton

You need to pad the day and month with a zero. Suppose today is March 8,
2006. To be able to fill a date picker that has data type "Date", you would
have to fill it from code with a string like "2006-03-08" (see the "0"
padding?). The JScript function you are using would just return "2006-3-8"
which will produce an unrecognizable date in InfoPath. Check out
http://enterprise-solutions.swits.net/infopath/date-time-basics-infopath.htm
to get a feel for using dates and times within InfoPath.

To solve your problem you could add a function like:

function addPadding(value)
{
var buffer = "";
buffer = "0" + value.toString();
return buffer.substr(buffer.length-2,2)
}

and then use it to set the date picker as follows:

var d = new Date();
var datePickerDate = d.getYear().toString() + "-" + addPadding(d.getMonth())
+ "-" + addPadding(d.getDate());
XDocument.DOM.selectSingleNode("//my:myDatePickerField").text =
datePickerDate;

Haven't tested this code, but it should work. You might have to remove the
"xsi:nil" attribute set on the date picker field when it does not have a
"Default Value" before setting its value to avoid getting an error.
 

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

Top