* Parse String to Date

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
mikej
Gold
Posts: 12
Joined: 07 Jun 2011 10:06
Family Historian: V7
Location: Wales

Parse String to Date

Post by mikej » 01 May 2020 08:18

Please, what is the way to convert a string holding a date and write it to a Date item?
I have tried various combinations of fhSetValueAsText and fhSetValueAsDate to no avail.
I have several hundred media items imported from another program. I am trying to extract a date entry from the item’s Note field and place it in the appropriate Date.
The Date string varies from an exact date, to a year, a circa, or a between range.
I have searched the Knowledge Base and Forum but have not found an answer.
I’m looking for something to replicate the process used when manually typing into the Date on the Property box from the keyboard.
Many thanks for any input.
Regards
MikeJ

User avatar
tatewise
Megastar
Posts: 27087
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Parse String to Date

Post by tatewise » 01 May 2020 10:59

Yes, handling Date formats is relatively complex.
The reference details are in the How to Write Plugins help pages under the The Family Historian API.
But instead of the Function Index where such as fhSetValueAsText and fhSetValueAsDate are documented, look in the Objects section where Date and Datept methods are documented.

So in your case, you need a script such as:
local dtDate = fhNewDate() create a new Date type
local bResult = dtDate:SetValueAsText(strText,true) convert Note text to a Date and allow Date Phrase
local bOK = fhSetValueAsDate(ptrDate,dtDate) save Date in OBJE.DATE field

If the Text is in a recognised FH Date format, then job done, otherwise it will save as a Date Phrase.
Then run the Fix Date Fields Plugin that converts Date Phrases holding a range of Date formats from other products into valid FH Date formats.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
mikej
Gold
Posts: 12
Joined: 07 Jun 2011 10:06
Family Historian: V7
Location: Wales

Re: Parse String to Date

Post by mikej » 01 May 2020 11:07

Thanks Mike, I will give that a try.
Regards
MikeJ

User avatar
mikej
Gold
Posts: 12
Joined: 07 Jun 2011 10:06
Family Historian: V7
Location: Wales

Re: Parse String to Date

Post by mikej » 01 May 2020 12:43

Thank you very much Mike. Worked first time!
:D
Regards
MikeJ

Post Reply