Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Date comparison
- This topic has 5 replies, 3 voices, and was last updated 1 year, 11 months ago by Brendan.
-
AuthorPosts
-
November 26, 2022 at 4:32 AM #48313
Ian WheelerParticipantHi Folks,
I am trying to check via a script whether a Date field contains a date that is earlier than today. If it is then I want to delete the record. Ideally this would be a manually run script. I am very new to TF (and loving it, and am in the process of migrating over from Filemaker) and Javascript. Below is the script that I have which just doesn’t work. I suspect it is my Date comparison element that is the problem as the Console.Log isn’t detecting any errors, but I don’t know how to fix it. Any help would be greatly appreciated
function Delete_Expired_Docs() { var records = form.getRecords(); var field_id = 'fld-46f16c00d13344e79478a8b629022ee7'; for (var index = 0, count = records.length; index < count; index++){ var aRecord = records[index]; var field_value = aRecord.getFieldValue(field_id); if (field_value < Date()) { form.deleteRecord(aRecord); } } form.saveAllChanges(); console.log(field_value); console.log(count); console.log(Date()); } Delete_Expired_Docs();
November 26, 2022 at 11:41 PM #48319
Daniel LeuParticipantAs an alternative, you could search for the records with an old date using advanced search:
Attachments:
You must be logged in to view attached files.November 27, 2022 at 12:12 AM #48321
Daniel LeuParticipantTo get the comparison to work, you need to get the numerical representation of the date. Additionally, the date object needs to set to the beginning of the day to properly detect a prior date.
function Delete_Expired_Docs() { var records = form.getRecords(); var field_id = 'fld-xxx'; // Get time in ms at beginning of today let now = new Date(); now.setSeconds(0); now.setMinutes(0); now.setHours(0); now.setMilliseconds(0); const today = now.getTime(); for (var index = 0, count = records.length; index < count; index++){ var aRecord = records[index]; var field_value = aRecord.getFieldValue(field_id); if (field_value.getTime() < today) { //form.deleteRecord(aRecord); console.log("found record"); } else { console.log("record doesn't match"); } } form.saveAllChanges(); } Delete_Expired_Docs();
BTW, I don’t like deleting records… I would just mark a record as ‘expired’. But that depends really on your data model.
November 29, 2022 at 2:07 AM #48347
Ian WheelerParticipantThanks Gents, I really appreciate your help with this, as a complete JS novice I would never have worked that out. I will implement your code when I get chance Daniel.
(P.S. I don’t usually delete database records but in this instance the database is a digital filing cabinet of home files. I am only deleting files that have either expired or have been superseded / renewed etc. Doing this reduces my file size which I am syncing across all of my devices.)November 29, 2022 at 3:06 AM #48348
BrendanKeymasterHi Ian,
Of course another way to delete these records is to just create a Saved Search that finds all records with the date field earlier than today using the
is before today
comparison operator. Then you can just select all the records and delete them with the Delete key. No programming needed.Thanks,
Brendan
November 29, 2022 at 3:07 AM #48349
BrendanKeymasterOh, also when you delete, your database file won’t necessarily get smaller. But you can shrink it by running the Compact Database function. Close and re-open your document after that to see the true new size of the database.
-
AuthorPosts
You must be logged in to reply to this topic.