Skip to main content
Kofax

Find Newest Date

17782
3011207

Question / Problem: 

Find Newest Date

Answer / Solution: 

The most recent date on a document can be found with the combination of a Format Locator and a Script Locator. The Format Locator finds all possible dates on the document as alternatives. The Script Locator loops over all alternatives of the Format Locator (all possible dates) and picks the newest date and copies this as its own alternative. A Date Formatter is used to get the actual date value from the date string.

This might be useful to find the invoice date on a document, which is after the order and ship date but before the due date. Of course it works only for current invoices, not for old documents. 

Attached is a project that will return the newest date from a document with US Date Format (MM/DD/YYYY). If required to use a different format, the DefaultDateFormatter will need to be adjusted. Here are the steps used to create the project:

  1. Create a new project and define a base class.
  2. Add a Format Locator named "FL_Dates" and add following regular expressions:
    • [0-3]?\d([\.\-/])[01]?\d\1([12]\d{3}|\d{2})
    • [0,3]?\d\.?§Months§([12]\d{3}|\d{2})
  3. Add a dictionary and name it Months. The attached project is using the file "English_Months_Abr.txt" from the directory C:\Program Files (x86)\Kofax\Transformation\Dict.
  4. Assign the Months dictionary in the "DefaultDateFormatter."
  5. Add a Script Locator, name it SL_Date and make use of the script below. Ensure the Script Locator is defined after FL_Dates.
  6. Create a field Date and assign it to be populated by SL_Date.
  7. Test this on the attached documents.

Below is the definition for the script locator SSL_Date. Ensure that the name of the Format Locator, the Script Locator and the Date Formatter are equal to the names in the script. 

Private Sub SL_Date_LocateAlternatives(pXDoc As CASCADELib.CscXDocument, pLocator As CASCADELib.CscXDocField)

   Dim pDate As CscXDocField
   Dim pDateAlt As CscXDocFieldAlternative
   Dim i As Long
   Dim j As Long
   Dim count As Long
   Dim MaxDate As Date
   Dim CurDate As Date
   Dim pTmpFeld As CscXDocField
   Dim MaxDateIndex As Long
   Dim pNewAlternative As CscXDocFieldAlternative

   MaxDate = Now - 6000
   MaxDateIndex = -1
   ' use actual Name of Format-Locator here, from where you want to take alternatives
   Set pDate = pXDoc.Locators.ItemByName("FL_Dates")
   count = pDate.Alternatives.Count
   For i = 0 To count - 1
      Set pDateAlt = pDate.Alternatives(i)

      Set pTmpFeld = New CscXDocField
      pTmpFeld.Text = pDateAlt.Text
      DefaultDateFormatter.FormatField pTmpFeld

      If pTmpFeld.DateFormatted = True Then
         ' suche neuestes Datum vor heute
         If pTmpFeld.DateValue > MaxDate And pTmpFeld.DateValue <= Now Then
            ' remember this value
            MaxDateIndex = i
            MaxDate = pTmpFeld.DateValue
         End If
      End If
   Next i

   ' copy alternative with newest date before today

   If MaxDateIndex > -1 Then
      Set pDateAlt = pDate.Alternatives(MaxDateIndex)
      pLocator.Alternatives.Create
      Set pNewAlternative = pLocator.Alternatives(0)

      ' alle Werte kopieren
      pNewAlternative.Text = pDateAlt.Text
      pNewAlternative.Left = pDateAlt.Left
      pNewAlternative.Top = pDateAlt.Top
      pNewAlternative.Width = pDateAlt.Width
      pNewAlternative.Height = pDateAlt.Height
      pNewAlternative.PageIndex = pDateAlt.PageIndex
      For j = 0 To pDateAlt.Words.Count - 1
         pNewAlternative.Words.Append(pDateAlt.Words(j))
      Next
      pNewAlternative.Confidence = pDateAlt.Confidence
   End If
End Sub

Applies to:  

Product Version Category
KTM 6.3 Scripting
KTM 6.2 Scripting

 

  • Was this article helpful?