Displaying the month in an InfoPath form

Someone asked this question on a discussion list and I thought the answer was one that others might be interested in.

What do you do if you want to set a field in an InfoPath form to show the month that the form is filled out?

This can be done by using the today() function. This function returns the date of the day that the function is called on. For example, if you call it on the 1st of March, it will return the value 2012-03-01 (you can change the way this is displayed, but this is the value pulled back by the function).

If you want to just display the month, rather than the full date, you can expand the formula using the substring function. This is a little function that takes a string and returns just a piece of it. So the function substring(today(), 6, 2) would return two characters of the string “2012-03-01”, starting with the sixth character. In this example, you’d get 03.

So that gives you the value for the month, but what if you want to see the text?

This requires another rule or, more accurately, twelve of them. Have a rule that looks at the value brought back by the previous formula and if it equals “01”, set the value of the month field to “January” and so on for the other eleven months.   

It’s probably easier to keep these two pieces slightly separate, rather than trying to do everything in one set. For example, you can have a field called month_value which is not displayed on the form. Create the first rule to populate this with the value substring(today(), 6, 2). This rule could be attached to form load behaviour, a button, another field, the submit action or various other items. However you choose to trigger it, it will populate this field with the number of the month.

Then you should have a second field, which is used to display the month name in the form. On this field, add the twelve rules to set the value of the field based on the value in month_value. You may want to set another condition that only does this if the month field is blank.

And make sure to make this field read-only otherwise someone will type something in and wreck your lovely rules.