Skip to main content
rickt38612460
Known Participant
January 8, 2023
Answered

Script to convert dates with ordinal indicators

  • January 8, 2023
  • 2 replies
  • 944 views

I have been using the script created originally by Oleh Melnyk and edited by @Manan Joshi  from https://community.adobe.com/t5/indesign-discussions/find-and-replace-date-format-indesign-cs6/td-p/13200572 to convert a date format from 09/05/1936 and replace it with 09 May 1936 which works great.

 

However I would like help to update the script to covert the date format from 09/05/1936 (dd/mm/yyyy or d/m/yyyy) and replace it with Saturday 9th May 1936.

 

At the moment I use the original script to convert the dates then add the week days manually and then use the following find & replace GREP commands to add the ordinal indicators with the GREP query runner by Peter Kahrel.

 

Find

(\<02\>)|(\<22\>)

Change to

$0nd

 

Find

(\<03\>)|(\<23\>)

Change to

$0rd

 

Find

(\<01\>)|(\<21\>)|(\<31\>)

Change to

$0st

 

Find

(\<04\>|\<05\>|\<06\>|\<07\>|\<08\>|\<09\>|\<10\>|\<11\>|\<12\>|\<13\>|\<14\>|\<15\>|\<16\>|\<17\>|\<18\>|\<19\>|\<20\>|\<24\>|\<25\>|\<26\>|\<27\>|\<28\>|\<29\>|\<30\>)

Change to

$0th

 

I think it is possible to use the JavaScript Date getDay() to get the correct weekday?

 

And I am hoping someone can help update the original script to do these additional steps of adding the correct week day and the ordinal indicators. The original script targets everything in the currently open/active document which is fine and works for most scenarios but I would also like a second version of the updated script that would target text boxes with a specific script label. Script Label = weekday_dates

 

Any help apreciated.

This topic has been closed for replies.
Correct answer Kasyan Servetsky

I assume the dates are located directly in text frames: I haven't tested it against text frames nested inside groups, anchored text frames, etc.
Here's my quick & dirty modification:

 

//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"];
        var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        var day = objDate.getDate();
        var suffix = "th";
        if (day % 10 === 1 && day !== 11) {
          suffix = "st";
        } else if (day % 10 === 2 && day !== 12) {
          suffix = "nd";
        } else if (day % 10 === 3 && day !== 13) {
          suffix = "rd";
        }
        return dayName[objDate.getDay()] + " " + day + suffix + " " + 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  textFrames = app.activeDocument.textFrames.everyItem().getElements(),
	 whereToSearch, foundPrep, foundElements, foundUnique, option;
	 
	 for(var f = 0; f < textFrames.length; f++){	
		if (textFrames[f].label == "weekday_dates"){
			whereToSearch = app.activeDocument.textFrames[f];
			foundPrep = whereToSearch.findGrep();
			foundElements = [];
			
			for(var x = 0; x < foundPrep.length; x++){
				foundElements.push(foundPrep[x].contents);
			}

			foundUnique = toUnique(foundElements);

			for(var i = 0; i < foundUnique.length; i++){
				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        
}

 

Before: two out of three text frames have the labels

After

 

2 replies

Participant
January 24, 2024

Hi

How i can change date format from 26-Jan-1949 to 26 Jan

rickt38612460
Known Participant
January 8, 2023

So quick update...

On a wimp I decided to aske ChatGPT https://openai.com/blog/chatgpt/

And it gave me this

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"];
  var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  var day = objDate.getDate();
  var suffix = "th";
  if (day % 10 === 1 && day !== 11) {
    suffix = "st";
  } else if (day % 10 === 2 && day !== 12) {
    suffix = "nd";
  } else if (day % 10 === 3 && day !== 13) {
    suffix = "rd";
  }
  return dayName[objDate.getDay()] + " " + day + suffix + " " + monthName[objDate.getMonth()] + " " + objDate.getFullYear();
}

Which I have updated in the scipt and amazingly it seams to be working. Still testing it and still shocked it is working. Full updated script below.

 

//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"];
        var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        var day = objDate.getDate();
        var suffix = "th";
        if (day % 10 === 1 && day !== 11) {
          suffix = "st";
        } else if (day % 10 === 2 && day !== 12) {
          suffix = "nd";
        } else if (day % 10 === 3 && day !== 13) {
          suffix = "rd";
        }
        return dayName[objDate.getDay()] + " " + day + suffix + " " + 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        
    } 
}

 

So if possible is somebody able to update this script to target all text boxes in a document with a
Script Label = weekday_dates 
instead of the whole documet?

Any help apreciated.

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
January 9, 2023

I assume the dates are located directly in text frames: I haven't tested it against text frames nested inside groups, anchored text frames, etc.
Here's my quick & dirty modification:

 

//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"];
        var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        var day = objDate.getDate();
        var suffix = "th";
        if (day % 10 === 1 && day !== 11) {
          suffix = "st";
        } else if (day % 10 === 2 && day !== 12) {
          suffix = "nd";
        } else if (day % 10 === 3 && day !== 13) {
          suffix = "rd";
        }
        return dayName[objDate.getDay()] + " " + day + suffix + " " + 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  textFrames = app.activeDocument.textFrames.everyItem().getElements(),
	 whereToSearch, foundPrep, foundElements, foundUnique, option;
	 
	 for(var f = 0; f < textFrames.length; f++){	
		if (textFrames[f].label == "weekday_dates"){
			whereToSearch = app.activeDocument.textFrames[f];
			foundPrep = whereToSearch.findGrep();
			foundElements = [];
			
			for(var x = 0; x < foundPrep.length; x++){
				foundElements.push(foundPrep[x].contents);
			}

			foundUnique = toUnique(foundElements);

			for(var i = 0; i < foundUnique.length; i++){
				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        
}

 

Before: two out of three text frames have the labels

After

 

rickt38612460
Known Participant
January 17, 2023

Thanks @Kasyan Servetsky your solution works as needed.

Appreciate you taking the time and looking at this.