【问题标题】:Indesign Javascript to convert datesIndesign Javascript 转换日期
【发布时间】:2022-12-13 06:58:10
【问题描述】:

我正在寻找一个 Adob​​e Indesign 脚本来转换日期格式。

例子25/12/2022 至 2022 年 12 月 25 日

我找到了执行此操作的脚本,但它使用的是美国日期格式 mm/DD/yyyy,我需要它使用英国日期格式 dd/MM/yyyy

原始脚本来自这个 Adob​​e 社区论坛,由 Oleh.melnyk 创建

https://community.adobe.com/t5/indesign-discussions/find-and-replace-date-format-indesign-cs6/m-p/13347884#M502699

寻找某人帮助将脚本更新/转换为英国日期格式 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        
    } 
}

我已经安装并使用了这个脚本,但需要将其转换为使用英国日期格式 dd/MM/yyyy

任何帮助表示赞赏。

【问题讨论】:

    标签: javascript adobe-indesign


    【解决方案1】:

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

    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() 方法从日期字符串中提取日期和年份。最后,日期使用月份名称、日期和年份进行格式化。

    【讨论】:

      【解决方案2】:

      更新后的脚本dd/mm/yyyy 或 d/m/yyyy可以在

      https://community.adobe.com/t5/indesign-discussions/find-and-replace-date-format-indesign-cs6/td-p/13200572

      及以下。

      //DESCRIPTION: Convert date format - finds 09/05/1936 and replace it with 09 May 1936
      #target indesign;
      
      /*
          by Oleh Melnyk at 5 April 2017
          requested at https://forums.adobe.com/message/9436296#9436296
          
          Edited By :- Manan Joshi
          Edited the date format to dd/mm/yyyy or d/m/yyyy
      */
      
      //> 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 t = date.split("/")
          date = t[1] + "/" + t[0] + "/" + t[2]
              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{1,2}/\d{1,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        
          } 
      }
      

      【讨论】:

      • 最好在您的答案中也添加解决方案的核心。因此,如果将来链接断开,人们仍然可以访问该解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 2010-12-18
      • 2021-06-07
      • 1970-01-01
      • 2018-09-29
      • 2010-10-20
      相关资源
      最近更新 更多