Community

Code Example - Converting a Two-digit Year to the Current Century

Sometimes it becomes necessary to work with ambiguous data that is extracted from a document. One example of this is with two-digit years within a date. Say the document contains a date of 12/21/36. Depending on the interpretation, the year could be seen as 1936, or 2036. Typically, you would enable field validation and check that the year is specified within a date range. If the date field happens to be repeated within a group or table, this would mean that the user might spend a lot of time manually adjusting the repeated field.
Another alternative would be to use a script rule to manipulate the date. The code example below assumes that the date being extracted is within the current century (2000) and the target 'culture' is in the US. To use this code, create a script rule within the field properties and ensure that the read-only checkbox in the Rule Settings is unchecked.

using System
using System.Globalization

string dateField = "Date1";
string extractedDate = Context.Field(dateField).Text;
DateTime result;

CultureInfo culture = new CultureInfo("en-US");
culture.Calendar.TwoDigitYearMax = 2099;
DateTime.TryParse(extractedDate, culture, DateTimeStyles.None, out result);

Context.Field(dateField).Text = result.ToString("MM/dd/yyyy");
0

Comments

0 comments

Please sign in to leave a comment.