Infopath_Display_Date_Datepart
InfopathDateTimeParts
How to use today() and now() functions to get parts of the date to display in a text box.
Go to Controls and insert a text box.
Right-click and select Text box properties
In the Data type select Text (time)
Under Default Value, click the function button fx next to the Value box. (You may be prompted to apply changes. Click OK).
In the formula box enter one of the following formulas according to your needs.
Using the today() function only returns the year month and day. To display the current Year-Month-Day.
Enter: today()
To display only the year.
Enter: substring-before(today(), “-“)
To display only the month number.
Enter: substring-before(substring-after(today(), “-“), “-“)
To display only the year and month.
Enter: concat(substring-before(today(), “-“), ” “, substring-before(substring-after(today(), “-“), “-“))
To display only the month number and day number separated by a dash.
Enter: substring-before(substring-after(today(), “-“), “-“)
To display only the day number.
Enter: substring-after(substring-after(today(), “-“), “-“)
Using the now() function returns the date and time separated by “T”. To display only the time.
Enter: substring-after(now(), “T”)
Notice we are using now() in this formula — now() returns a Date and Time, while today() returns just a date. If you don’t care for military time, set the field to a Time data type and use the format button in the field’s properties to select a different format (the field above is set that way).
To display only the hours.
Enter: substring-before(substring-after(now(), “T”), “:”)
To display only the hours in military time.
Enter: substring-before(substring-after(now(), “T”), “:”) mod 12
Mod operator to the rescue. The remainder of the hour divided by 12 will get us the non-military time afternoon hours:
I think I still need to fix this. I put mod 13 to get it to show 12 at noon. Otherwise, it shows 0 (zero) at noon. The Mod operator is used to display military time.
The remainder of the hour divided by 12 will get us the non-military time afternoon hours. If you don’t care for military time, set the field to a Time data type
and use the format button in the field’s properties to select a different format (the field above is set that way).
To display only the minutes.
Enter: substring-before(substring-after(now(), “:”), “:”)
To display only the seconds.
Enter: substring-after(substring-after(now(), “:”), “:”)