转换日期的InDesign Javascript

tquggr8v  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(101)

I am looking for an Adobe Indesign script to convert date formats.

Example 25/12/2022 to 25 December 2022

I found script which does exactly this but it is using the American date format mm/DD/yyyy and I need it use the UK date format dd/MM/yyyy
The original script came from this Adobe Community forum and was created by Oleh.melnyk
https://community.adobe.com/t5/indesign-discussions/find-and-replace-date-format-indesign-cs6/m-p/13347884#M502699
Looking for someone to help update/convert the script to the UK Date format dd/MM/yyyy

//DESCRIPTION: Convert date format - finds 09/05/1936 and replac eit with 05 September 1936
#target indesign;

/*
    by Oleh Melnyk at 5 April 2017
    requested at https://forums.adobe.com/message/9436296#9436296
*/

//> START OF doUndoWraper
if (parseFloat(app.version) < 6) // "app.version < 6" if it's running under an earlier version than CS4, as earlier versions don't support "Undo" in scripts
    doUndoWrapper();
else
    app.doScript(doUndoWrapper, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Date Format");
//< END OF doUndoWraper

function doUndoWrapper(){
    function toUnique(a, b, c) { //array,placeholder,placeholder
        b = a.length;
        while (c = --b)
            while (c--) a[b] !== a[c] || a.splice(c, 1);
        return a // not needed ;)
    }
    
   function convertDateFormat(date){
        var objDate = new Date(date);
        var monthName = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        return  ("0" + objDate.getDate()).slice(-2) + " " + monthName[objDate.getMonth()] + " " + objDate.getFullYear();
    }

    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear settings

    app.findChangeGrepOptions.includeLockedLayersForFind = false; // search in Locked Layers
    app.findChangeGrepOptions.includeLockedStoriesForFind = false; // search in Locked Stories
    app.findChangeGrepOptions.includeHiddenLayers = false; // search in HiddenLayers
    app.findChangeGrepOptions.includeMasterPages = false; // search in Master Pages
    app.findChangeGrepOptions.includeFootnotes = true; // search in Footnotes

    app.findGrepPreferences.findWhat = "\\d{2}\/\\d{2}\/\\d{4}";

    var whereToSearch = app.activeDocument; // default - search in entire document
    var foundPrep = whereToSearch.findGrep();
    
    var foundElements = [];
    for(var x = 0; x < foundPrep.length; x++){
         foundElements.push(foundPrep[x].contents); 
    }

    var foundUnique = toUnique(foundElements);

    for(var i = 0; i < foundUnique.length; i++){                
        var option = foundUnique[i];                
        app.findGrepPreferences.findWhat = option;           
        app.changeGrepPreferences.changeTo = convertDateFormat(option);        
        whereToSearch.changeGrep();        
        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear find what and change to field        
    } 
}

I have installed this script and used it but need it converted to use the UK Date format dd/MM/yyyy
Any help appreciated.

ukqbszuj

ukqbszuj1#

您可以结合使用字串操作和日期格式化技术。

const date = '25/12/2022';

// Parse the date string into a Date object
const dateObj = new Date(date);

// Get the month name from the Date object
const monthName = dateObj.toLocaleString('default', { month: 'long' });

// Extract the day and year from the date string
const day = date.substring(0, 2);
const year = date.substring(6);

// Format the date using the month name, day, and year
const formattedDate = `${day} ${monthName} ${year}`;

console.log(formattedDate); // 25 December 2022

在此范例中,日期字串会先使用Date建构函式剖析成Date对象。然后使用toLocaleString()方法取得字串形式的月份名称。之后,会使用substring()方法从日期字串撷取日期和年份。最后,会使用月份名称、日期和年份来格式化日期。

相关问题