Convert dates to GEDCOM standard form

44 views
Skip to first unread message

Jay Fletcher

unread,
Jan 17, 2024, 11:52:41 AMJan 17
to GEDitCOM II Discussions
Hello,

Has anyone written a script to convert dates from U.S. numerical (mm/dd/yyyy) format to GEDCOM standard (DD MMM YYYY) format? If so, would you be willing to share what you have done? I would love to make use of what other people have started rather than starting from scratch. 

Thanks,
Jay

Jim Eggert

unread,
Jan 17, 2024, 1:53:17 PMJan 17
to geditcom-ii...@googlegroups.com
I think GEDitCOM II can already read dates in US numerical format on GEDCOM import and interpret them properly. You have to have the GCII preferences set correctly:
Settings > Dates > uncheck Assume dd/mm/yyyy

=Jim
> --
> You received this message because you are subscribed to the Google Groups "GEDitCOM II Discussions" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to geditcom-ii-discu...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/geditcom-ii-discussions/95072d9d-921f-4a63-9212-4510a6420c14n%40googlegroups.com.

John Nairn, Developer

unread,
Jan 18, 2024, 12:18:52 PMJan 18
to GEDitCOM II Discussions
Yes, GEDItCOM II allows you to use non-standard dates and will read them correctly using its "Smart Dates" feature (perhaps to be trendy it should be rebranded as "AI Dates"). This, if using GEDitCOM II, there is no need to convert dates to the GEDCOM standard. Some other issues are:

1. GEDitCOM II philosophy is to not change data without permission. Thus it will keep (and correctly use) dates entered in non-GEDCOM style (such as MM/DD/YYYY). It will even keep dates it cannot understand, but will not be able to use them. You can use the "Check Dates..." report to find all dates that cannot be understood.

2. The main reason you might need to convert dates is to export a GEDCOM file to be read by other software that is not so "Smart" in reading dates. It would be a relatively easy easy script (using "date parts" and "Date Text" commands). I will give it a try.

Regards,
John Nairn

Jay Fletcher

unread,
Jan 18, 2024, 8:44:50 PMJan 18
to GEDitCOM II Discussions
Hi John,

Thanks for your reply. I wrote the following script, and it seems to work. I really appreciate having the script editor for GEDitCOM II.

Regards,
Jay

! Reformat US Dates to GEDCOM (Internal Script for GEDitCOM II)
! Created: 2024-01-18 03:50:24 +0000
! Author: Jay Fletcher
!
! Description: This script finds dates formated in US format (MM/DD/YYYY)
! and reformats them according to GEDCOM 5.5.1 (dd MMM yyyy)

!>>>>>>> Subroutines
 
!>>>>>>> Main Script

! Script name and version requirement
#scriptName="Reformat US Dates to GEDCOM"

! Check version and find front document
PreChecks #scriptName,3.1,1,1,1
gcapp.get gdoc,frontDocument

#numConverted = 0

! get individual records
gdoc.get irecs,"individuals"
#numRecs=@irecs.count

! Process the records
gdoc.beginUndo

#fractionStepSize=0.01
#nextFraction=#fractionStepSize

Repeat "#i",0,#numRecs-1
  irecs.#i.get rec
  rec.findStructures dates,"DATE"
  Repeat "#j",0,@dates.count-1
    date = @dates.#j.contents
    Words "#dateComponents",1,3,date,"/"
    if #dateComponents[0] = 3
      DateStyle "#date",date,"%d %M %y"
      dates.#j.set "contents" #date
      #numConverted+=1
    endif
  EndRepeat
! time for progress
  #fractionDone = (#i+1)/#numRecs
  if #fractionDone>#nextFraction
    NotifyProgress #fractionDone
    #nextFraction+=#fractionStepSize
  endif
EndRepeat

gdoc.endUndo,"Reformat US Dates to GEDCOM"

! alert when done
if #numConverted==1
  #msg="Converted one US date to GEDCOM format."
else if #numConverted==0
  #msg="No US dates converted to GEDCOM format."
else
  #msg="Converted "&#numConverted&" US dates to GEDCOM format."
endif
UserOption "#var",#scriptName&" done",#msg,"OK"

John Nairn

unread,
Jan 19, 2024, 8:08:02 PMJan 19
to geditcom-ii...@googlegroups.com
Hi Jay,

Very interesting. I also started writing one and very similar to yours. The only potential issue I can see in yours is if some data is unintelligible (such as text “bad date” in a date field). I think one of the commands might cause the script to abort. There are two fixes:

1. Before running the script, run the Change Dates… report. It will find any bad dates to fix before running the script

2. Check dates in the script before converting them.

I will post a script using the second option (and I plan to include it in the next version of GEDitCOM II as well).

Thanks for the idea.

Regards,
John Nairn

John Nairn, Developer

unread,
Jan 20, 2024, 2:53:05 PMJan 20
to GEDitCOM II Discussions
Hi Jay,

Below is the script I wrote. This works even if dates are illegal dates such as "bad date". It converts them into a legal date "INT (bad date)" so no potential information is lost. I did test on all possible GEDCOM date styles.

Note that this scripts uses a feature of the DateParts command I forgot to document in the help. If it is feed an invalid date, it returns an error message explaining what is wrong with the date. When there is an error, parts[0]=1 and parts[1] is the error. A valid data will always have parts[0]=5 (although some might be empty). I will add the missing documentation to next version of GEDitCOM Editor.

Finally, I wrote this using GEDitCOM II, 3.1 (beta). This version has some changes to internal scripting. This script will probably work in 3.0 version too. Or get beta version from this link:


Regards, John

! Convert to GEDCOM Dates (Internal Script for GEDitCOM II)
! Created: 2024-01-18
! Author: John Nairn
!
! Description: This script will changes dates in all (or selected)
! records to GEDCOM style dates. It is not needed for files used in
! GEDitCOM II (because it recognizes many date styles), but it might
! be useful if one finds a file with non-standard dates and might
! want to export those dates to a good GEDCOM file.


! Script name and version requirement
#scriptName="Convert to GEDCOM Dates"

! Need a document open
PreChecks #scriptName,3,1,1,1
gcapp.get gdoc,frontDocument

! Decide which records to search
#msg="Convert dates in 'All' records in the file or just the currently 'selected' records."
UserOption "#whichOnes","Which records should be changed?",#msg,"All","Cancel","Selected"
if #whichOnes="All"
  gdoc.get recs,gedcomRecords
else if #whichOnes="Selected"
  gdoc.get recs,selectedRecords
else
  exit
endif

#numRecs = @recs.count
#numDates = 0
#fractionStepSize=0.05
#nextFraction=#fractionStepSize

gdoc.beginUndo
Repeat "#i",0,#numRecs-1
  recs.#i.get rec
  rec.findStructures dates,"DATE"
  #numd=@dates.count
  Repeat "#j",0,#numd-1
    ! divide this date into parts
    dates.#j.get date
   
    ! Split valid dates into parts
    ! (an invalid date will have one part with error message)
    #oldDate=@date.contents
    DateParts "#parts",#oldDate
    if #parts[0]>1
      ! prefix word
      #newDate = #parts[1]
      ! first date (but could be empty)
      if #parts[2]!=""
        if #newDate!=""
          #newDate&=" "
        endif
        #newDate&=#parts[2]
      endif
      ! if conjuction assume as two dates
      if #parts[3]!=""
        #newDate&=" "&#parts[3]&" "&#parts[4]
      endif
      ! Date coimment
      if #parts[5]!=""
        #newDate&=" "&#parts[5]&""
      endif
    else
      ! Convert bad dates to a valid date comment
      #newDate="INT ("&#oldDate&")"
    endif
   
    ! only convert dates that have changed
    if #newDate!=#oldDate
      date.set contents,#newDate
      #numDates+=1

    endif
  EndRepeat
 
  ! time for progress
  #fractionDone = #i/#numRecs

  if #fractionDone>#nextFraction
    NotifyProgress #fractionDone
    #nextFraction+=#fractionStepSize
  endif
EndRepeat
gdoc.endUndo "Date Conversions"
NotifyProgress 1

! output summary of the results
if #numDates==0
  #msg = "No dates needed conversion to the GEDCOM date style"
else
  if #numDates==1
    #msg = "One date was converted to GEDCOM style date"
  else
    #msg = #numDates&" dates were converted to GEDCOM style dates"
  endif
endif
UserOption "#done",#msg,"","OK"


Jay Fletcher

unread,
Jan 28, 2024, 11:38:00 AMJan 28
to GEDitCOM II Discussions
Thank you, John. And thank you for fixing the issue with the Census Districts in Canadian Provinces.

Jay

Reply all
Reply to author
Forward
0 new messages