• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Extendscript and Date Formats

Contributor ,
May 17, 2023 May 17, 2023

Copy link to clipboard

Copied

I think this will be a difficult question.

I have a document that must have a date as 20230517 in one location and as 5 May 2023 in another location.

This is a publication date, but not necessarily today's date, nor the creation date.

We use user variables - for example "DateYearFirst" and "DateD-M-Y" for the two dates and update them manually.

I know if VBA, there are format codes that can change the date format.

Does anyone know of a way to set up either Extendscript or the template so that when the YYYYMMDD variable is updated, the d Mmm YYYY date variable is also updated?
Something like an OnChange event for the variable.

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 7 Correct answers

Community Expert , May 18, 2023 May 18, 2023

I poked around at this and couldn't find an event that is triggered when a variable value is changed. So I would probably do it this way with a script:

 

Display a simple "Set Date" dialog box when either a book or document is active. You would enter the date in the YYYYMMDD format. When you click OK, the script would update (or create) both variables based on the value entered.

 

The script would have to parse the YYYYMMDD date in order to get the correct value for the DateD-M-Y variable, but E

...

Votes

Translate

Translate
Mentor , May 18, 2023 May 18, 2023

Hi Marshall, I don't think there is a direct notification. You might be able to trap it with Constants.FA_Note_PreFunction... you would have to experiment to see if iparm gives you something reliable. But since you are already writing code, why not just create your own variable editor? You could create a dialog box with 3 simple fields for the day, month, and year, then update both variables programmatically with the input. That seems the optimal approach. This would give you the added benefit o

...

Votes

Translate

Translate
Community Expert , May 18, 2023 May 18, 2023
#target framemaker

var doc, variable;

doc = app.ActiveDoc;
variable = doc.GetNamedVarFmt ("Current Date (Long)");
variable.Fmt = "<$year>";

Votes

Translate

Translate
Community Expert , May 22, 2023 May 22, 2023

I would probably just code a simple parser and lookup.

20230522

year = 2023

month = 05

day = 22

 

Lookup for month:

...

04 = Apr

05 = May

06 = Jun

...

etc.

 

That might be less complicated than messing with the Javascript Date object.

Votes

Translate

Translate
Community Expert , May 22, 2023 May 22, 2023

The datepicker won't work in ExtendScript because it uses a widget that is meant to be displayed in a browser.

Votes

Translate

Translate
Community Expert , May 24, 2023 May 24, 2023

I recommend downloading this PDF:

https://creativepro.com/files/kahrel/indesign/scriptui.html

There used to be a dedicated ScriptUI Forum on Adobe Forums, but I am not sure it is still active. You might want to check and see if it is still there.

Votes

Translate

Translate
Contributor , Jun 01, 2023 Jun 01, 2023

I figured it out, but the code could I'm sure be cleaner and more simplified, but it works.

I started with this code:

function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}
var myDate = new Date('2013-02-11');
var newDate = addDays(myDate,5);

From https://stackoverflow.com/questions/14842526/add-days-to-date-format-yyyy-mm-dd Note that although the thread specifically asks for format YYYYMMDD, and the example shows YYYY-MM-DD, it doesn't actually work, it returns

...

Votes

Translate

Translate
Contributor ,
May 25, 2023 May 25, 2023

Copy link to clipboard

Copied

@frameexpert Rick - cross-posting with https://community.adobe.com/t5/indesign-discussions/validating-data-in-scriptui/m-p/13817022#M528102 - but ...
Your conversion code works great - although I don't really understand it.
Would you mind putting together a code snippet to verify a string is a valid YYYYMMDD date. I have links to code in the other thread that doesn't work, and it seems like your code would pretty much be doing this to do the conversion.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 25, 2023 May 25, 2023

Copy link to clipboard

Copied

I figured it out!!!

Used a combination of Rick's code and the nscaler code:

var date, dateObj;

date = "20230525";

dateObj = IsValidDate(date);
alert (dateObj);


function IsValidDate(date) {
// Finds dates between 2000 and 2199 as valid.    
    var regex, matches, dateObj;
    
    // Regular expression for parsing YYYYMMDD date.
    regex = /^(\d{2})(\d{2})(\d{2})(\d{2})$/;
    
    if (regex.test (date) === true) {
        matches = date.match (regex);
        dateObj = {};
        dateObj.century = matches[1];
        if (dateObj.century < 20 || dateObj.century > 21){
            return false;
            }
        dateObj.partyear = matches[2];
        dateObj.year = dateObj.century + dateObj.partyear;
        dateObj.monthPadded = matches[3];
        dateObj.month = Number (dateObj.monthPadded.replace (/^0+/, ""));
        dateObj.dayPadded = matches[4];
        dateObj.day = Number (dateObj.dayPadded.replace (/^0+/, ""));
        // Define the number of days per month
        var daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        // Leap years
        if (dateObj.year % 400 == 0 || (dateObj.year % 100 != 0 && dateObj.year % 4 == 0)) {
            daysInMonth[2] = 29;
        }
        if (dateObj.month < 1 || dateObj.month > 12 || dateObj.day < 1 || dateObj.day > daysInMonth[dateObj.month]) {
            return false;
        }
    return true;   
     } return false;
}

Probably not the cleanest way to do it, but it works!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 31, 2023 May 31, 2023

Copy link to clipboard

Copied

I'm back again, I'm having a hard time finding a function to add 1 year or 2 years to a specified YYYYMMDD date.

 

IOW, I'm looking for something like:

var date, newdate;

date = "20230525"

function addDays(date, days) {

// some code;

return result;

}

newdate = addDays(date, 365) should return 20240530.

newdate = addDays(date, 730) should return 20240530.

There are a lot of examples at https://stackoverflow.com/questions/563406/how-to-add-days-to-date but the one that ALMOST works is:

var theDate = new Date(20230531);
var myNewDate = new Date(theDate);
myNewDate.setDate(myNewDate.getDate() + 365);
alert(myNewDate);

but it comes out a month ahead.

I think I need to convert the date into some format that JS understands, then get the millisecond date from that using GetDate, then use SetDate to add the days, then convert the output back to YYYYMMDD - but I'm pretty fuzzy on how to do that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 01, 2023 Jun 01, 2023

Copy link to clipboard

Copied

LATEST

I figured it out, but the code could I'm sure be cleaner and more simplified, but it works.

I started with this code:

function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}
var myDate = new Date('2013-02-11');
var newDate = addDays(myDate,5);

From https://stackoverflow.com/questions/14842526/add-days-to-date-format-yyyy-mm-dd Note that although the thread specifically asks for format YYYYMMDD, and the example shows YYYY-MM-DD, it doesn't actually work, it returns a date in 1916 (!!!???!!!), but if you change the input to MM-DD-YYYY, it works properly, although it just gives you the GMT value.

I combined that with Rick's parser that was posted previously and some code that I stole (errrm acquired) from https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-3.php and came up with:

var date, newdate;
date = "20230601"
var newdate = addDays(date, 365);
alert(newdate);

function addDays(date, days) {
    var regex, matches, dateObj;
    // Regular expression for parsing YYYYMMDD date.
    regex = /^(\d{4})(\d{2})(\d{2})$/;  
    if (regex.test (date) === true) {
        matches = date.match (regex);
        dateObj = {};
        dateObj.year = matches[1];
        dateObj.monthPadded = matches[2];
        dateObj.dayPadded = matches[3];
        var initialDate=dateObj.monthPadded + "-" + dateObj.dayPadded + "-" + dateObj.year
        var myDate = new Date(initialDate)
        var ResultDate = new Date(myDate.getTime() + days*24*60*60*1000)
        var dd = ResultDate.getDate();
        var mm = ResultDate.getMonth()+1; 
        var yyyy = ResultDate.getFullYear();
        if(dd<10) {
            dd='0'+dd;
        } 
        if(mm<10) {
            mm='0'+mm;
        } 
        var outputdate = yyyy+mm+dd;        
        return outputdate;        
        } 
}

Like I said, I'm sure there are more efficient ways to do it, but it works!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines