programmatically set date

J

Jeffshex

I'm trying to find out if there is a way to set the date to automatically use
the previous Sunday.
For example if today is Wednesday the 24, it will fill in 8/21/2005.
Is this possible?

Thanks,
Jeff
 
S

Scott L. Heim [MSFT]

Hi Jeff,

Here is a link to a site that provides sample JScript functions for
calculating dates and times. One of the functions demonstrates how to move
to a specific day of the week - although the sample provides the "Friday"
date, you should be able to modify this for your needs.

Date and Time Arithmetic in JScript
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvid/html/
msdn_vidateadd.asp

I hope this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffshex

Cool, I think I have it edited so that it works for Sunday.
Now how do I go about putting that in?
I'm researching it now, but not coming up with anything good as to do this.

Do I need to create some more code that defines the node I'm attempting to
set?
Just need some more info I guess.

Thanks again Scott.
-Jeff
 
S

Scott L. Heim [MSFT]

Hi Jeff,

Yes - you will need to get a reference to the date field "node" on your
form. As an example, I dropped a Date Picker control on a new, blank form
and set the name to: myDate.

I added a button to the form and on the click event of the button I have
the following code:

var objMyDate = XDocument.DOM.selectSingleNode("//my:myFields/my:myDate");
if(objMyDate.getAttribute("xsi:nil"))
objMyDate.removeAttribute("xsi:nil");
objMyDate.text = "2005-08-24";

** NOTE: The check for "xsi:nil" is needed...see the following article:

826998: You receive an error message if you try to programmatically set the
text value of an XML node in InfoPath 2003
http://support.microsoft.com/default.aspx?scid=kb;EN-US;826998

I hope this helps you!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffshex

Instead of a button, can I just toss it in the Form Load Event?

Something like this maybe:

function XDocument::OnLoad(eventObj)
{
startDate = new Date();
if(startDate.getDay() == 7){
// It's Sunday today, just subtract one week.
startDate.setDate( startDate.getDate() - 7);
}
else {
// Walk backwards a day at a time until we get to Sunday.
while(startDate.getDay() != 7){
startDate.setDate( startDate.getDate() - 1);
}
}
return startDate;


var objMyDate = XDocument.DOM.selectSingleNode("//my:myFields/my:myDate");
if(objMyDate.getAttribute("xsi:nil"))
objMyDate.removeAttribute("xsi:nil");
objMyDate.text = startDate;

}
 
S

Scott L. Heim [MSFT]

Hi Jeff,

I did not test it there but you may need to put that in the "OnSwitchView"
event as the Load event may be too early.

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffshex

I set the code into the SwitchView event. I says there is an error saying
'null' is null or not an object.
It points to this line of code:
if(objMyDate.getAttribute("xsi:nil"))

So, I'm gettin' lost in the wonderful world of code :(
Any ideas as to what I'm doing wrong?

-Jeff

function XDocument::OnSwitchView(eventObj)
{
// Set HeadDate to the previous Sunday.

var objMyDate = XDocument.DOM.selectSingleNode("//my:myFields/my:HeadDate");
if(objMyDate.getAttribute("xsi:nil"))
objMyDate.removeAttribute("xsi:nil");
objMyDate.text = startDate;

startDate = new Date();
if(startDate.getDay() == 7){
// It's Sunday today, just subtract one week.
startDate.setDate( startDate.getDate() - 7);
}
else {
// Walk backwards a day at a time until we get to Sunday.
while(startDate.getDay() != 7){
startDate.setDate( startDate.getDate() - 1);
}
}
return startDate;
}
 
J

Jeffshex

Hmm...that code (the calculation part) keeps running and doesn't stop. It
asks if I want to continue running the script. If you say yes, it will lock
the program up.
Any other examples that might work?

-Jeff
 
S

Scott L. Heim [MSFT]

Hi Jeff,

You will need to include a test in the OnSwitchViews event as follows:

if(XDocument.View.Name == "View 1")
{
var objMyDate = XDocument.DOM.selectSingleNode("//my:myFields/my:myDate");
if(objMyDate.getAttribute("xsi:nil"))
objMyDate.removeAttribute("xsi:nil");
objMyDate.text = "2005-08-24";
}

Obviously, you would need to replace "View 1" with the name of your view
that has the Date Picker control where you want to set the date.

How does this work?

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffshex

Scott. I had to make a slight adjustment to the code. The date field isn't
bound to the DB so it's setup as ("//my:myDate");
This works here:

if(XDocument.View.Name == "View 1")
{
var objMyDate = XDocument.DOM.selectSingleNode("//my:HeadDate");
if(objMyDate.getAttribute("xsi:nil"))
objMyDate.removeAttribute("xsi:nil");
objMyDate.text = "2005-08-24";
}

Now I gotta figure out the last sunday script that doesn't hang the pc.
 
S

Scott L. Heim [MSFT]

Hi Jeff,

Have you been able to get this working? If not, here is what works for me:

- In my OnSwitchView event, I have the following:

if(XDocument.View.Name == "View 1")
{
var objMyDate = XDocument.DOM.selectSingleNode("//my:myDate");
var objSunday = XDocument.DOM.selectSingleNode("//my:mySunday");

if(objMyDate.getAttribute("xsi:nil"))
objMyDate.removeAttribute("xsi:nil");
objMyDate.text = "2005-08-22";

if(objSunday.getAttribute("xsi:nil"))
objSunday.removeAttribute("xsi:nil");

//Call the date calc function and concatenate the pieces together for
use in JScript date calculations
objSunday.text = lastSunday(objMyDate.text.substr(5,2) + '/' +
objMyDate.text.substr(8,2) + '/' + objMyDate.text.substr(0,4));
}

Then I have this stand-alone function that is called from the above code
(this is modified from the web site I sent to you:)

function lastSunday(dtDate)
{
startDate = new Date(dtDate);
if(startDate.getDay() == 0)
{
// It's Sunday today, just subtract one week.
startDate.setDate( startDate.getDate() - 7);
}
else
{
// Walk backwards a day at a time until we get to Sunday.
while(startDate.getDay() != 0){
startDate.setDate( startDate.getDate() - 1);
}
}

var myYear = startDate.getYear();
var myMonth = startDate.getMonth() + 1;

if(myMonth < 10)
myMonth = "0" + myMonth;

var myDay = startDate.getDate();
if(myDay < 10)
myDay = "0" + myDay;

startDate = myYear + '-' + myMonth + '-' + myDay;
return startDate;
}

I hope this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffshex

Scott,

You snuck a node in on me that I wasn't aware of. Where did the mySunday
come from if we already have a myDate"?
 
J

Jeffshex

On top of that other question I just asked about the mySunday field, I see
that in the code we are piecing together those 2 fields to give us the
previous Sunday.
Is this going to be a problem if that myDate field is always set to
"2005-08-22".
Since the code is using that value, won't it always return the same Sunday?
Does that make sense?
Let me know what you think.

-Jeff
 
S

Scott L. Heim [MSFT]

Hi Jeff,

Sorry - this was just a demonstration to show one Date Time picker control
would have one date and another (mySunday) would have the prior Sunday's
date. If you are just trying to set one control with the prior Sunday's
date then the additional control is not needed.

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Scott L. Heim [MSFT]

Hi Jeff,

I'm really getting confused...if the date is *always* going to be 8/22, why
do you need to set this in code?? You can just set this as a default value?

At any rate, no it won't be a problem. In order to do date calculations
with JScript, the date needs to be in a specific format - that is why I
needed to bring those pieces together.

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffshex

I was just referring to this line of code that you had shown me earlier:

if(objMyDate.getAttribute("xsi:nil"))
objMyDate.removeAttribute("xsi:nil");
objMyDate.text = "2005-08-22";

I wasn't sure why you had that there, nor was I sure if that affected the
outcome of the lastSunday() code.
 
S

Scott L. Heim [MSFT]

So were you able to get this working as you need?

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffshex

As I was typing up a reply saying no it doesn't quite work yet, I had an
idea. It worked, but can you tell me if this is going to always work.
I have only one node now called mySunday and in the properties of that node
I set the default value to the today() function.
I modified the code that pieces together the date calculation to look like
this:

function XDocument::OnSwitchView(eventObj)
{
if(XDocument.View.Name == "View 1")
{
var objSunday = XDocument.DOM.selectSingleNode("//my:mySunday");


if(objSunday.getAttribute("xsi:nil"))
objSunday.removeAttribute("xsi:nil");

//Call the date calc function and concatenate the pieces together for use
in JScript date calculations
objSunday.text = lastSunday(objSunday.text.substr(5,2) + '/' +
objSunday.text.substr(8,2) + '/' + objSunday.text.substr(0,4));
}

It returned the 21st...which is correct, but what do you think...did I
finally get it?

-Jeff
 
S

Scott L. Heim [MSFT]

I do believe you got it! :)

Take care,

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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