Skip to main content
Inspiring
May 17, 2023
Answered

Extendscript and Date Formats

  • May 17, 2023
  • 2 replies
  • 5177 views

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.

    This topic has been closed for replies.
    Correct answer Marshall_Brooks

    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.


    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!

    2 replies

    Legend
    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 of making it foolproof... that is, add validation that makes it impossible to configure an incorrect format.

     

    Russ

    frameexpert
    Community Expert
    Community Expert
    May 18, 2023

    Hi Russ. I tried using the post function and note-back-to-user notifications, but neither showed anything when I updated a user variable value. -Rick

    Legend
    May 18, 2023

    OK, thanks. I didn't test it. But I have to tell you, that idea about a custom editor is pure genious 🙂

     

    Russ

    frameexpert
    Community Expert
    Community Expert
    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 ExtendScript (Javascript) has date functions that make this pretty easy.

    Inspiring
    May 22, 2023

    The script would have to parse the YYYYMMDD date in order to get the correct value for the DateD-M-Y variable, but ExtendScript (Javascript) has date functions that make this pretty easy.

    Would you have 

     

    The script will be fairly complicated, but ...

    There are actually three dates - the issue date in YYYYMMDD format, the issue date in  d MMM YYYY format, and what I'll call an Expiration date of +1 or +2 years minus 1 day from the issue date (and NOT accounting for leap years). There are also other variables to write, which are simple enough to change.

     

    I'm thinking I want the pop-up form to display the current variables - which would be 20XXMMDD for the issue and expiration dates. The user can change the Issue date and there should be a pop-up calendar for this. There will be a calculate function for the Expiration Date, and the DD MMM YYYY variable will be populated automatically without being displayed.

    For a datepicker, I found: https://www.npmjs.com/package/js-datepicker - haven't tested it yet, but it looks like what I am wanting.

    For date validation, I found https://www.scaler.com/topics/date-validation-in-javascript/ - but it lists three methods and I don't know which one is preferable. DateParse seems the simplest.

    For the Expiration Date Calculation - https://www.tutorialrepublic.com/faq/how-to-add-days-to-current-date-in-javascript.php - SetDate + 365 or + 730 seems to give me what I am looking for.
    I think I'm on the correct track, but I was wondering if anyone knew any better options.

    Inspiring
    May 22, 2023

    I couldn't find a converter for YYYYMMDD to DD MMM YYYY - but it seems easy enough to parse and recompose.