Date field error during File Renaming & Feature Request: Date field customized option

edited July 19, 2024
Hi,
I have the following issues during "Rename File using Parent Meta-data"
1. The date field is incorrectly recognized when the date is dd/mm/yyyy or mm/dd/yyyy or dd month yyyy.
2. If a customized option is available in the date field during File renaming, it would be useful in creating a file in standard ISO format 1806 "yyyy-mm-dd". (E.g. {{date format="yyyy-mm-dd"}}.

Thanks.
  • I provided the JavaScript in case anyone needs to identify non-compliant article date format in your library. The script will create a "non-compliant date" tag for articles that do not follow the "yyyy-mm-dd" format. Once you identify the articles with non-compliant dates using the tag selector (as shown in the figure below), you can modify the dates accordingly.
    https://s3.amazonaws.com/zotero.org/images/forums/u7630663/td574xtymf069rvu451k.png

    In Zotero, go to Tools > Developer > Run JavaScript.
    Copy and paste the script into the console and Run (Select the option: Run as async function)

    //Java Script for identifying non-compliance of date in format "yyyy-mm-dd"
    // Fetch all items from the user library
    var items = await Zotero.Items.getAll(Zotero.Libraries.userLibraryID);
    var nonCompliantDateTag = "non-compliant date";

    for (let item of items) {
    try {
    let date = item.getField('date');
    if (date && !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
    await item.addTag(nonCompliantDateTag);
    await item.save();
    }
    } catch (e) {
    console.log(`Error accessing date field for item ${item.id}: ${e.message}`);
    }
    }

    return "Tagging completed.";

    //Java Script for identifying non-compliance of date in format "dd-mm-yyyy"
    // Fetch all items from the user library
    var items = await Zotero.Items.getAll(Zotero.Libraries.userLibraryID);
    var nonCompliantDateTag = "non-compliant date";

    for (let item of items) {
    try {
    let date = item.getField('date');
    if (date && !/^\d{2}-\d{2}-\d{4}$/.test(date)) {
    await item.addTag(nonCompliantDateTag);
    await item.save();
    }
    } catch (e) {
    console.log(`Error accessing date field for item ${item.id}: ${e.message}`);
    }
    }

    return "Tagging completed.";



  • A modified version of the non-compliance-date tag creation javascript (with the removal of tag after update)
    =======
    Note: Unselect Run as async function

    (async () => {
    try {
    console.log("Fetching items...");
    // Fetch all items from the user library
    var items = await Zotero.Items.getAll(Zotero.Libraries.userLibraryID);
    console.log(`Fetched ${items.length} items.`);

    var nonCompliantDateTag = "non-compliant date";
    var updatedItems = [];

    // Function to process each item
    const processItem = async (item) => {
    try {
    let date = item.getField('date');
    if (date) {
    console.log(`Item ${item.id} date: ${date}`);
    }
    if (date && !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
    console.log(`Item ${item.id} has non-compliant date: ${date}`);
    await item.addTag(nonCompliantDateTag);
    } else {
    console.log(`Item ${item.id} has compliant date: ${date}`);
    await item.removeTag(nonCompliantDateTag);
    }
    updatedItems.push(item);
    } catch (e) {
    console.log(`Error accessing date field for item ${item.id}: ${e.message}`);
    }
    };

    // Process all items
    await Promise.all(items.map(item => processItem(item)));

    // Save updated items within a transaction
    await Zotero.DB.executeTransaction(async () => {
    await Promise.all(updatedItems.map(item => item.saveTx()));
    });

    console.log("Tagging completed.");
    } catch (e) {
    console.log(`Error during processing: ${e.message}`);
    }
    })();

Sign In or Register to comment.