14.1. Date Calculations

One of the tasks that come in handy with some InfoPath forms is working with dates. The user interface doesn't really provide you with the ability to work with dates, but with scripting or managed code (C#) you can do so.

An example of date manipulation is taking today's date and displaying what tomorrow's date will be, or next week's, next month's, and even a year from today. You can see the values displayed in a form in Figure 14-1.

Figure 14.1. Figure 14-1

14.1.1. Simple Date Calculations Using Script

In both cases of creating the InfoPath form for displaying dates, you will be creating the form itself and then manipulating the InfoPath object model using code either in a script or as managed code. When discussing the code that is created using scripting, the first task to perform is to store today's date in a variable, using the following line of code:

var date = new Date();

14.1.1.1. JScript Date Object

The JScript Date object has a number of methods that can be used for extracting parts of the specified date. If you use the Date object as shown in the preceding line of code, the current system date is returned and, in this case, stored in the date variable.

Some of the methods that will be used for this section are shown in the following table:

MethodDescription
getDateRetrieves the current numeric day of the month of the Date object.
GetFullMonthRetrieves the current month of the Date object.
getFullYearReturns the four-digit year from the Date object.

Before using the various methods of the Date object, you must create a reference to an InfoPath control for each of the controls used.

14.1.1.2. Creating a Reference to an InfoPath Field
var todayField = XDocument.DOM.selectSingleNode("//my:TodaysDate");

Using the InfoPath object model, discussed in Chapter 12, "Getting Starting Using Scripts," you will use the main object, XDocument. The selectSingleNode method, part of the DOM (Document Object Model), is used to retrieve a reference to the XPath value of the name of the control passed to it, in this case "//my:TodaysDate".

14.1.1.3. Assigning a Value to a Variable

Everything that is now done with the todayField variable will be reflected in the field on the form. So, assigning a value to the Text property of the variable assigns the value to the field on the form, as in the following line of code:

todayField.text = (date.getMonth() + 1) + "/" + date.getDate() + "/" +
       date.getFullYear();

You also can see the Date object methods used in the last line of code. The last line of code changes based on what you are trying to accomplish. That line placed a formatted version of today's date into the todayField control. To add a day to the current date, you use the following line of code, which assigns it the variable called tomorrowsField.

tomorrowsField.text = (date.getMonth() + 1) + "/" + (date.getDate() + 1) + "/" +
       date.getFullYear();

14.1.1.4. Try It Out: Creating a Form That Manipulates Dates Using Script

The first task you have to do is create a form to display the information:

  1. Open InfoPath.

  2. Click on Design a Form.

  3. Click on New Blank Form.

  4. Lay out the form as displayed in Figure 14-2.

    Figure 14.2. Figure 14-2
  5. Name the fields down the form as follows: TodaysDate, Tomorrow, NextWeek, NextMonth, and NextYear.

  6. Choose On Load Event from the ToolsProgramming... menu. The Microsoft Script Editor opens with the on load event shell displayed.

  7. Type in the following code between curly brackets of the function:

    // create a variable storing today's date
    var date = new Date();
    
    // create references to the InfoPath form controls
    var todayField = XDocument.DOM.selectSingleNode("//my:TodaysDate");
    var tomorrowsField = XDocument.DOM.selectSingleNode("//my:Tomorrow");
    var nextWeekField = XDocument.DOM.selectSingleNode("//my:NextWeek");
    var nextMonthField = XDocument.DOM.selectSingleNode("//my:NextMonth");
    var nextYearField = XDocument.DOM.selectSingleNode("//my:NextYear");
    
    // create the new date values and store them in the controls.
    todayField.text = (date.getMonth() + 1) + "/" + date.getDate() + "/" +
          date.getFullYear();
    tomorrowsField.text = (date.getMonth() + 1) + "/" + (date.getDate() + 1) + "/" +
          date.getFullYear();
    nextWeekField.text = (date.getMonth() + 1) + "/" + (date.getDate() + 7) + "/" +
          date.getFullYear();
    nextMonthField.text = (date.getMonth() + 2) + "/" + date.getDate() + "/" +
          date.getFullYear();
    nextYearField.text = (date.getMonth() + 1) + "/" + date.getDate() + "/" +
         (date.getFullYear() + 1);

    The screen will then look as shown in Figure 14-3.

    Figure 14.3. Figure 14-3
  8. Save and close the editor.

  9. Click Preview Form. The form is displayed with the various dates, as shown in Figure 14-1.

The reason that these are simple date calculations is that you are really just adding values to the parts of the dates, not looking at the date values. For instance, if you add a month to the current month, and the month is December, then you see an error in the displayed field. Now check out how to do it using C# and Visual Studio .NET to resolve this issue.

14.1.2. Date Calculations Using C# and Visual Studio .NET

One of the huge benefits of using C# instead of JScript is that with C# you have the power of the classes in .NET. Instead of using the limited Date object in JScript, you get to use the powerful DateTime .NET class, including all the properties and methods belonging to it. These methods include specific operations for adding days, months, and years to dates.

As with the script example, you will assign today's date to a variable, as shown here:

System.DateTime date = System.DateTime.Today;

14.1.2.1. System.DateTime Class

The DateTime class has a number of very nice methods for working with dates. You can see a number of them in the following table, which are used for this example:

MethodDescription
TodayReturns a DateTime object representing today's date.
ToShortDateStringReturns a string value formatted in the short date format of mm/dd/yy.
AddDaysAdds the number of days passed in an argument to the DateTime object it is called from.
AddMonthsAdds the number of months passed in an argument to the DateTime object it is called from.
AddYearsAdds the number of years passed in an argument to the DateTime object it is called from.

Remember that these are methods of a DateTime object. You will see them used shortly. First check out how to create a reference in your InfoPath form fields.

14.1.2.2. Creating a Reference to an InfoPath Field

Unlike using Jscript, when you use a variable in C# you need to declare it as a specific type. In the case of assigning a reference to a field on the InfoPath form, it will be a IXMLDOMNode type. The command itself looks very similar to the same line of code in JScript. Here is one of the lines of code you will use:

IXMLDOMNode todayField = thisXDocument.DOM.selectSingleNode("//my:TodaysDate");

Using the InfoPath object model in C#, discussed in Chapter 13, "Working with .NET Managed Code," you will use the main object, XDocument. The selectSingleNode method, part of DOM, is used to retrieve a reference to the XPath value of the name of the control passed to it, in this case "//my:TodaysDate".

By declaring the variable as an IXMLDOMNode object you will be able to see the properties and methods using Intellesense, also introduced in earlier chapters.

14.1.2.3. Assigning a Value to a Variable

Everything that is now done with the todayField variable will be reflected in the field on the form, as can be shown the following line of code:

todayField.text = date.ToShortDateString();

This line of code stores the value in the date variable in the text property of the todayField, formatting it using the ToShortDateString method. The line of code after this one varies depending on which field is being assigned.

14.1.2.4. Try It Out: Manipulating Dates Using C#

As with the last Try It Out, the first task you have to do is create a form to display the information:

  1. Open Visual Studio .NET 2003.

  2. Click New Project. The New Project dialog box will open.

  3. Choose Microsoft Office InfoPath Projects.

  4. Click Visual C# Projects. The InfoPath project will be displayed as shown in Figure 14-4.

    Figure 14.4. Figure 14-4
  5. Click OK. The Microsoft Office Project Wizard will be displayed.

  6. Click the Finish button, accepting the defaults. The project will be created with a new blank form displayed.

  7. Lay out the form as displayed in Figure 14-5.

    Figure 14.5. Figure 14-5
  8. Choose On Load Event from the ToolsProgramming... menu choice. The new event will be created, and you will be placed in the routine.

  9. Type the following between the opening and closing brackets.

    // Assign today's date to a variable.
    System.DateTime date = System.DateTime.Today;
    
    // Assign the field references.
    IXMLDOMNode todayField = thisXDocument.DOM.selectSingleNode("//my:TodaysDate");
    IXMLDOMNode tomorrowsField = thisXDocument.DOM.selectSingleNode("//my:Tomorrow");
    IXMLDOMNode nextWeekField = thisXDocument.DOM.selectSingleNode("//my:NextWeek");
    IXMLDOMNode nextMonthField = thisXDocument.DOM.selectSingleNode("//my:NextMonth");
    IXMLDOMNode nextYearField = thisXDocument.DOM.selectSingleNode("//my:NextYear");
    
    // Assign the new values to the form fields.
    todayField.text = date.ToShortDateString();
    tomorrowsField.text = date.AddDays(1).ToShortDateString();
    nextWeekField.text = date.AddDays(7).ToShortDateString();
    nextMonthField.text = date.AddMonths(1).ToShortDateString();
    nextYearField.text = date.AddYears(1).ToShortDateString();

  10. Choose DebugStart. The form will then open in Preview Mode, shown in Figure 14-6.

    Figure 14.6. Figure 14-6

If you change the system date to a later month, you will notice that unlike the form created in the script version, all the dates are displayed correctly. Again, this is one of the benefits of using C# over scripting. While you could jump through more hoops (meaning create more code) to accomplish the same goal, it is easier to use .NET to accomplish the task.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.84