How to Compare Dates Correctly in Microsoft Word Field Codes
Question details
An IF field code comparing a merge date with the current date always returns a false result, regardless of the actual dates.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Executing a mail merge or generating dynamic document text using an IF field to compare chronological dates.
- Observed behavior
- The conditional IF field evaluates the comparison incorrectly, defaulting to the false statement output instead of processing the date logic properly.
Ensure your document contains the correct merge fields and that you are familiar with toggling field codes on and off using the Alt + F9 keyboard shortcut.
Apply a Numeric Date Picture Switch to the Field Codes
Format both the merge date and the current date as YYYYMMDD so the word processor compares the values as sortable integers rather than text strings.
By default, word processors evaluate field code outputs as text strings. When standard date formats like MM/DD/YYYY or DD/MM/YYYY are compared, the logic fails because the first characters dictate the sorting order, ignoring the actual chronological year or month.
To resolve this, apply a date picture switch (\@ YYYYMMDD) to both date fields. This transforms the dates into straight numerical values (e.g., 20231025), allowing the IF field's greater-than or less-than operators to calculate the result accurately.
Open your document and press Alt + F9 on your keyboard to reveal all hidden field codes, transforming the regular text into formulas.
Locate your MERGEFIELD and insert the switch \@ YYYYMMDD right before the closing bracket. It should look like this: { MERGEFIELD my_date \@ YYYYMMDD }.
Locate the DATE field in your formula and apply the exact same switch. It should look like this: { DATE \@ YYYYMMDD }.
Nest the fields correctly inside your IF code: { IF { MERGEFIELD my_date \@ YYYYMMDD } > { DATE \@ YYYYMMDD } "True Result" "False Result" }.
Select the entire field block, press F9 to update the data, and then press Alt + F9 again to hide the codes and view your correctly evaluated result.

Use WPS Writer for Accurate Mail Merges and Date Comparisons
WPS Writer provides a highly compatible and powerful environment for advanced document automation. You can easily execute complex date comparisons, IF statements, and mail merges using standard field code syntax directly within the app.
- 1. Open your document in WPS Writer: Launch WPS Office and open the document where you need to perform the date comparison or mail merge.
- 2. Insert a blank field: Navigate to the Insert tab and click 'Field', or simply press Ctrl + F9 to insert the necessary curly brackets.
- 3. Input the logic formula: Type your IF statement, formatting both date fields with \@ YYYYMMDD just as you would in standard field code syntax.
- 4. Toggle and update: Right-click the field and select 'Toggle Field Codes', then press F9 to refresh the calculation and display the correct true/false output.

Frequently Asked Questions
Why do standard date formats fail when used in IF fields?
Standard date formats (like DD/MM/YYYY) are treated as literal text by the application. In text comparison, a date like '01/12/2023' evaluates as less than '02/01/2020' simply because the character '1' comes before '2', entirely ignoring the year. The YYYYMMDD format turns the date into a logical mathematical integer for precise comparison.
Can I format the output text dynamically instead of just 'a' or 'b'?
Yes. Inside the quotation marks for your true and false results, you can include plain text, additional field codes, or differently formatted dates (e.g., { MERGEFIELD my_date \@ "MMMM d, yyyy" }). The YYYYMMDD switch is only strictly required for the mathematical comparison part of the IF statement.
How do I force the field code to refresh after I change the formula?
To refresh a field code, click anywhere inside the field (or press Ctrl + A to select the entire document) and press the F9 key. If you are currently viewing the raw field codes, you must first press Alt + F9 to switch back to the standard text view before the update becomes visible.




