InfoPath 2010 date and time picker control basics

Applies to: InfoPath 2010

Learn how to add a Date and Time Picker control to an InfoPath 2010 form template and then set the value of the Date and Time Picker control using rules or code.

ADVERTISEMENTS

InfoPath 2010 introduced a new control, the Date and Time Picker, with which users can enter both a date and a time on an InfoPath form.

A Date and Time Picker control is a control that consists of an InfoPath date picker and a text box, and that is a field that has a Data type of Date and Time (dateTime).

As written in a previous article about date and time basics in InfoPath, InfoPath stores dates and times (behind the scenes) in one of 3 formats.

If you have a date of for example 1 August 2010 at 4:25 p.m., the 3 possible formats would be:

  1. 2010-08-01 (date only format)
  2. 2010-08-01T16:25:00 (date and time format)
  3. 16:25:00 (time format)

This is important to know if you want to use rules or write code to set the value of a Date and Time Picker control in InfoPath 2010.

Add a Date and Time Picker to an InfoPath 2010 form template

To add a Date and Time Picker control to an InfoPath 2010 form template:

  1. Open InfoPath Designer 2010.
  2. On the File tab, click New.
  3. Double-click Blank Form.
  4. Place the cursor anywhere on the InfoPath form template where you want to add the Date and Time Picker.
  5. On the Home tab under the Controls group, click Date and Time Picker.

Set the value of a Date and Time Picker to today's date when the form opens

To automatically have a Date and Time Picker control on an InfoPath 2010 form be populated with today's date and the current time:

  1. Follow the instructions in the previous section on how to add a Date and Time Picker to an InfoPath 2010 form template.
  2. Select the Date and Time Picker, and then click the Properties tab.
  3. On the Properties tab under the Properties group, click Default Value.
  4. On the Field or Group Properties dialog box on the Data tab, click the function button behind the Value field.
  5. On the Insert Formula dialog box, type now() in the Formula text box, and click Verify Formula. If your formula does not contain any errors, click OK to close the message box, and then click OK to close the Insert Formula dialog box.
  6. On the Field or Group Properties dialog box, click OK.

You can also watch a video tutorial on how to set the default value of an InfoPath date and time picker.

You can also use Rules to set the value of a Date and Time Picker in InfoPath 2010. To set the value of a Date and Time Picker when the InfoPath form opens:

  1. Follow the instructions in the previous section on how to add a Date and Time Picker to an InfoPath 2010 form template.
  2. On the Data tab under the Rules section, click Form Load.
  3. On the Rules pane, click New, and then select Action from the drop-down list.
  4. In the details section for the rule, click Add, and then select Set a field's value from the drop-down list.
  5. On the Rule Details dialog box, click the fields button behind the Field text box.
  6. On the Select a Field or Group dialog box, select the field that represents the Date and Time Picker on the InfoPath form, and click OK.
  7. On the Rule Details dialog box, click the function button behind the Value field.
  8. On the Insert Formula dialog box, type now() in the Formula text box, and click Verify Formula. If your formula does not contain any errors, click OK to close the message box, and then click OK to close the Insert Formula dialog box.
  9. On the Rule Details dialog box, click OK.

Note: If you want to manually enter a date and time as a value for the rule instead of using the now() function, the value must have the format yyyy-MM-ddThh:mm:ss, so for example 2010-10-13T14:05:00. Do not use the Insert Formula dialog box, but enter the date directly into the Value text box on the Rule Details dialog box.

Programmatically set the value of a Date and Time Picker in InfoPath 2010

To programmatically set the value of a Date and Time Picker in InfoPath 2010:

  1. Follow the instructions in the section on how to add a Date and Time Picker to an InfoPath 2010 form template.
  2. On the Home tab under Controls, click Button to add a button control to the InfoPath form template.
  3. On the Developer tab, click Code Editor to add a Clicked event handler for the button in code.
  4. Type the following C# code in the body of the Clicked event handler you just added:

    XPathNavigator root = MainDataSource.CreateNavigator();

    XPathNavigator field1Node = root.SelectSingleNode(
    "/my:myFields/my:field1", NamespaceManager);

    if (field1Node.MoveToAttribute("nil", NamespaceManager.LookupNamespace("xsi"))
    field1Node.DeleteSelf();

    field1Node.SetValue("2010-10-13T14:05:00");

    where field1 represents the Date and Time Picker on the InfoPath form and 2010-10-13T14:05:00 the value that the Date and Time Picker will be set to.
  5. Save your project and build it.
 


Related InfoPath Articles:

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services