8 Replies Latest reply on Oct 8, 2017 3:44 AM by Colin Flashman

    Utility for determining the month of the date

    Anitha Selvaraj Level 2

      Hi All,

       

      I am currently using the Date object to get the date in numbers and then by using a switch case, am manually printing the month in words.

       

      Is there any utility/method which gives us the month text like August, September, etc., instead of 8 and 9?

       

      Please give me some pointers on the same.

       

      Thanks,

      Anitha

        • 1. Re: Utility for determining the month of the date
          Marijan Tompa [tomaxxi] Level 4

          Hey!

           

          Yeah, there is an easy way to achieve this!

          You can create prototype for Date function like this:

           

          Date.prototype.dayName = function() {
              var myDay = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
              return myDay[this.getDay()];
          }
          
          Date.prototype.monthName = function() {
              var myMonth = ['January','February','March','April','May','June','July','August','September','October','November','December'];
              return myMonth[this.getMonth()];
          }
          

           

          And you are using it like this:

           

          var myToday = new Date;
          alert((myToday.dayName() + ', ' + myToday.getDate() + ' ' + myToday.monthName() + ', ' + myToday.getFullYear()));
          

           

          Hope it helps!

           

          --

          tomaxxi

          http://indisnip.wordpress.com/

          • 3. Re: Utility for determining the month of the date
            Mukhbet Level 1

            I got a question near to this one:

            I want to create a Diary note book, it needs Day names at entire 365 days of year in 365 pages, it is very time consuming to enter these day names manually. is there any automatic method to achieve this?

            • 4. Re: Utility for determining the month of the date
              lf.corullon Level 2

              I have this code, wrote by Jongware, and translated to Portuguese (BR) by me.

              I think this could do what you want.

               

              #target indesign
              /*======================================================
                  SCRIPT BY [Jongware]
                  EDITADO E COMENTADO POR LFCorullón
                  CONTATO E SUPORTE: lf@corullon.com.br
              ======================================================*/
              
              
              /*======================================================
                  NA LINHA ABAIXO, VOCÊ PODE EDITAR O NOME DOS MESES
                  PARA O MELHOR FORMATO PARA A SUA NECESSIDADE.
                  
                  ATENTE PARA MANTER AS ASPAS.
              ======================================================*/
              var months = [ "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" ];
              /*======================================================
                  A LINHA ABAIXO REFERE-SE À QUANTIDADE DE DIAS EM CADA MÊS CITADO NA LINHA DE CIMA.
              ======================================================*/
              var length = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
              /*======================================================
                  NA LINHA ABAIXO, VOCÊ PODE EDITAR O NOME DOS DIAS DA SEMANA
                  PARA O MELHOR FORMATO PARA A SUA NECESSIDADE.
                  
                  ATENTE PARA MANTER AS ASPAS.
              ======================================================*/
              var days = [ "Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado" ];
              count = 0;
              day = 0;
              month = 0;
              /*======================================================
                  ABAIXO DO COMENTÁRIO, NA LINHA 43, ONDE DIZ WEEKDAY, TROQUE
                  O NÚMERO APRESENTADO PELO NÚMERO REFERENTE AO DIA EM QUE
                  O 1º DE JANEIRO CAIRÁ NO ANO PARA O QUAL VOCÊ ESTÁ MONTANDO
                  A AGENDA SEMANAL.
              =======================================================
                  0 = DOMINGO
                  1 = SEGUNDA-FEIRA
                  2 = TERÇA-FEIRA
                  3 = QUARTA-FEIRA
                  4 = QUINTA-FEIRA
                  5 = SEXTA-FEIRA
                  6 = SÁBADO
              ======================================================*/
              weekday = 5;
              str = "";
              while (count < 365)
              {
               count++;
               str += days[weekday]+", "+(day+1)+" de "+months[month]+"\r";
               day++;
               if (day >= length[month])
               {
                day = 0;
                month++;
                if (month > 11)
                 month = 0;
               }
               weekday = (weekday+1) % 7;
              }
              app.selection[0].contents = str;
              
              • 5. Re: Utility for determining the month of the date
                Colin Flashman Adobe Community Professional

                Luis' solution is good, but requires knowing the calendar you're after ahead of time as several lines of the script need to be changed depending on your circumstances.

                If you have Microsoft Excel, there's a script-free way of doing this, and that is in cell A1, type in the date of 1 Jan <year that you want>. From here, in cell A2, type =A1+1. If done correctly, it will say 2 Jan <year that you want>. Copy this cell all the way through to to line 365 (366 if a leap year). Then, highlight the column and use the format cells feature of microsoft excel to display the date exactly as you want it to appear. This type can then either be copied and pasted into InDesign or placed as a file. If you keep this excel solution on-file, it can be quickly repurposed for the next year by changing the start date to the appropriate year.

                While this isn't a script, it's a handy solution given that the way InDesign handles the date object for beginner scripters like me is annoying. Using the excel solution will allow the formatting of the date to be changed on-the-fly so you can quickly go from DD/MM/YYYY to USA/Canada dates MM/DD/YYYY to ISO dates YYYY/MM/DD and even have the dates in full, partial... whatever date formatting Excel allows for.

                • 6. Re: Utility for determining the month of the date
                  Mukhbet Level 1

                  Guys thanks for replying, my main problem is that I want to have a variable text box in the master page which runs the script, the script which incrementally insert like saturday, sunday, monday... friday at the header of all my pages. as I said, I am designing a diary which requires sat-friday or whatever mon-sun in the header. I can do it for example by entering manually, but it takes too long and is boring, also I missed one, I have to go from start. see, it is not feasible.

                  I say there should be a method that does insert day names in a text box variable.

                  • 7. Re: Utility for determining the month of the date
                    Colin Flashman Adobe Community Professional

                    I do have this script on the shelf that I've been working on but there's no error correction in the user interface - it incorrectly allows for dates such as 31 february.... however if you enter realistic dates into the UI, it will work fine. Just have to remove some commas at the end.

                    //DESCRIPTION:Creates two week ahead calendar-planners for diaries.
                    #target "indesign"
                    var storedPreset = app.documentPresets[0].createPrimaryTextFrame;  
                    app.documentPresets[0].createPrimaryTextFrame = true;  
                    var win = new Window("dialog", 'Day of the week maker', undefined, );
                    with(win){
                        win.r1 = add( "group", undefined, undefined );
                        with(win.r1){
                            var startDate = "";
                    var dates = [];
                    var days = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",];
                    var ajsndays = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"];
                    var feblydays = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"];
                    var febregdays = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"];
                            var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                            win.r1.start = add( "statictext", undefined, 'Start Date (day, month, year)' );
                            win.r1.startValue = add( "edittext", undefined, startDate );
                                            win.r1.startValue.onChange = onInputNumberChange;
                                            function onInputNumberChange() 
                                                {
                                                win.r1.startMonth.enabled = true;
                    
                                                }
                                            win.r1.startValue.characters =4;
                            win.r1.startMonth = add("dropdownlist", undefined, months);
                            win.r1.startMonth.selection = 0;
                            win.r1.startMonth.enabled = false;
                                            win.r1.startMonth.onChange = onstartMonthChange;
                                            function onstartMonthChange() 
                                                {
                                                win.r1.startDay.enabled = true;
                                                var sMth = win.r1.startMonth.selection.index;
                                            }
                            win.r1.startDay = add("dropdownlist", undefined, days);
                            win.r1.startDay.selection = 0;
                            win.r1.startDay.enabled = false;
                                            win.r1.startDay.onChange = onstartDayChange;
                                            function onstartDayChange() 
                                                {
                                                win.r2.endValue.enabled = true;
                    
                                                }
                         }
                        win.r2 = add( "group", undefined, undefined );
                        with(win.r2){
                            win.r2.end = add( "statictext", undefined, 'End Date (day, month, year)' );
                            var stopDate = "";
                            win.r2.endValue = add( "edittext", undefined, stopDate );
                                            win.r2.endValue.onChange = onendValueChange;
                                            function onendValueChange() 
                                                {
                                                win.r2.endMonth.enabled = true;
                    
                                                }
                            win.r2.endValue.enabled = false;
                            win.r2.endValue.characters = 4;
                            win.r2.endMonth = add("dropdownlist", undefined, months);
                            win.r2.endMonth.enabled = false;
                            win.r2.endMonth.selection = 0;
                                            win.r2.endMonth.onChange = onendMonthChange;
                                            function onendMonthChange() 
                                                {
                                                win.r2.endDay.enabled = true;
                                                var eMth = win.r2.endMonth.selection.index;
                                            }
                            win.r2.endDay = add("dropdownlist", undefined, days);
                            win.r2.endDay.enabled = false;
                            win.r2.endDay.selection = 0;
                    }
                    win.r3 = add( "group", undefined, undefined );
                        with(win.r3){
                                            var Exit = 0;
                            win.r3.OK = add( "button", undefined, 'OK' );
                            win.r3.Cancel = add( "button", undefined, 'Cancel' );
                                            win.r3.Cancel.onClick = function()
                                            {
                                    win.close();
                                    Exit = 1;                           
                                            }
                            }
                        }
                    win.center();
                    win.show();
                        if(Exit ==1)
                    {
                            this.exit();
                        }
                    Date.prototype.addDays = function(days) {
                        var dat = new Date(this.valueOf())
                        dat.setDate(dat.getDate() + days);
                        return dat;
                    }
                                                var startfinal = (String(win.r1.startDay.selection + 1)) + " " + (String(win.r1.startMonth.selection)) + " " + (String(win.r1.startValue.text));
                                                startResult = new Date(startfinal);
                                                startDate = startResult;
                                                var endfinal = (String(win.r2.endDay.selection + 1)) + " " + (String(win.r2.endMonth.selection)) + " " + (String(win.r2.endValue.text));
                                                var endResult = new Date(endfinal);
                                                stopDate = endResult;
                        var dateArray = new Array();
                    var myDay = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");  
                    var dotw = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31")
                    var myMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");  
                    var d=[];
                    var e=[];
                    var f=[];
                    var g=[];
                        var currentDate = startDate;
                        while (currentDate <= stopDate) {
                            dateArray.push( new Date (currentDate) )
                            var myLetterDateCaller = currentDate.getDay();
                            var myLetterDate = myDay[myLetterDateCaller];
                            var myGoodDate = currentDate.getDate();
                            var myMonthDateCaller = currentDate.getMonth();
                            var myMonthDate = myMonth[myMonthDateCaller];
                            d.push(myGoodDate);
                            e.push(myLetterDate);
                            f.push(myMonthDate);
                              currentDate = currentDate.addDays(1);
                      }
                    alert(d.length);
                    var bkp = app.marginPreferences.properties;  
                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                    app.marginPreferences.properties = {  
                         top:10, left:10, bottom:10, right:10,  
                         columnGutter:0, columnCount:1  
                         };  
                    var myDoc = app.documents.add(true, undefined,  
                         {documentPreferences: {pageWidth:'210mm', pageHeight:'297mm', facingPages: false, createPrimaryTextFrame: true}}  
                         );  
                    app.marginPreferences.properties = bkp;  
                    var myTextFrame = myDoc.pages[0].textFrames[0];//myDoc.pages.item(0).textFrames.add({geometricBounds:[10, 10, 287, 200]});  
                    var myText = myTextFrame.texts[0];  
                    for (i = 0; i < d.length; i++){
                    var textReport = e[i] + ", " +d[i] +" " +f[i]+"\r";//only if you want to add the year to the top
                    g.push(textReport) };
                    g.join("\r");
                    myTextFrame.contents += g;
                    app.documentPresets[0].createPrimaryTextFrame = storedPreset;  
                    
                    • 8. Re: Utility for determining the month of the date
                      Colin Flashman Adobe Community Professional

                      Actually, I'll do everyone a favor. I've fixed the user interface error correction and made it so that the date appears once on each page. If you want to make further adjustments, you now have the code:

                      //DESCRIPTION:Creates day to a page for a planner.
                      #target "indesign"
                      var storedPreset = app.documentPresets[0].createPrimaryTextFrame;  
                      app.documentPresets[0].createPrimaryTextFrame = true;  
                      var win = new Window("dialog", 'Day of the week maker', undefined, );
                      with(win){
                          win.r1 = add( "group", undefined, undefined );
                          with(win.r1){
                              var startDate = "";
                      var dates = [];
                      var days = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",];
                      var ajsndays = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"];
                      var feblydays = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"];
                      var febregdays = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"];
                              var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                              win.r1.start = add( "statictext", undefined, 'Start Date (year, month, day)' );
                              win.r1.startValue = add( "edittext", undefined, startDate );
                                              win.r1.startValue.onChange = onInputNumberChange;
                                              function onInputNumberChange() 
                                                  {
                                                  win.r1.startMonth.enabled = true;
                                                  }
                                              win.r1.startValue.characters =4;
                              win.r1.startMonth = add("dropdownlist", undefined, months);
                              win.r1.startMonth.enabled = false;
                                              win.r1.startMonth.onChange = onstartMonthChange;
                                              function onstartMonthChange() 
                                                  {
                                                 var startAsNo = parseInt(win.r1.startValue.text, 10);
                                                   win.r1.startDay.enabled = true;
                                                  var sMth = win.r1.startMonth.selection.index;
                                                  if (sMth === 3 || sMth === 5 || sMth === 8 || sMth === 10){;
                                                  win.r1.startDay.removeAll();
                                                  for (var i = 0; i < ajsndays.length; i++){
                                                     win.r1.startDay.add('item', ajsndays[i]);}
                                                 
                                                 }else if (sMth === 0 || sMth === 2|| sMth === 4  || sMth === 6 || sMth === 7 || sMth === 9 || sMth === 11){;
                                                  win.r1.startDay.removeAll();
                                                  for (var i = 0; i < days.length; i++){
                                                     win.r1.startDay.add('item', days[i]);}
                                                 }
                                                  else if (sMth === 1 && startAsNo %4 ==0){;
                                                  win.r1.startDay.removeAll();
                                                  for (var i = 0; i < feblydays.length; i++){
                                                     win.r1.startDay.add('item', feblydays[i]);}
                                                 }
                                                  else if (sMth === 1 && startAsNo %4 !=0){;
                                                  win.r1.startDay.removeAll();
                                                  for (var i = 0; i < febregdays.length; i++){
                                                     win.r1.startDay.add('item', febregdays[i]);}
                                                 }
                                              }
                              win.r1.startDay = add("dropdownlist");
                              win.r1.startDay.minimumSize = [50,20];
                              win.r1.startDay.enabled = false;
                                              win.r1.startDay.onChange = onstartDayChange;
                                              function onstartDayChange() 
                                                  {
                                                  win.r2.endValue.enabled = true;
                                                   win.r2.endValue.text = win.r1.startValue.text;
                                                 win.r2.endMonth.enabled = true;
                                                  }
                           }
                          win.r2 = add( "group", undefined, undefined );
                          with(win.r2){
                              win.r2.end = add( "statictext", undefined, 'End Date (year, month, day)' );
                              var stopDate = "";
                              win.r2.endValue = add( "edittext", undefined, stopDate );
                              win.r2.endValue.enabled = false;
                              win.r2.endValue.characters = 4;
                              win.r2.endMonth = add("dropdownlist", undefined, months);
                              win.r2.endMonth.enabled = false;
                                              win.r2.endMonth.onChange = onendMonthChange;
                                              function onendMonthChange() 
                                                  {
                                                 var endAsNo = parseInt(win.r2.endValue.text, 10);
                                                   win.r2.endDay.enabled = true;
                                                  var eMth = win.r2.endMonth.selection.index;
                                                  if (eMth === 3 || eMth === 5 || eMth === 8 || eMth === 10){;
                                                  win.r2.endDay.removeAll();
                                                  for (var i = 0; i < ajsndays.length; i++){
                                                     win.r2.endDay.add('item', ajsndays[i]);}
                                                 
                                                 }else if (eMth === 0 || eMth === 2 || eMth === 4 || eMth === 6 || eMth === 7 || eMth === 9 || eMth === 11){;
                                                  win.r2.endDay.removeAll();
                                                  for (var i = 0; i < days.length; i++){
                                                     win.r2.endDay.add('item', days[i]);}
                                                 }
                                                  else if (eMth === 1 && endAsNo %4 ==0){;
                                                  win.r2.endDay.removeAll();
                                                  for (var i = 0; i < feblydays.length; i++){
                                                     win.r2.endDay.add('item', feblydays[i]);}
                                                 }
                                                  else if (eMth === 1 && endAsNo %4 !=0){;
                                                  win.r2.endDay.removeAll();
                                                  for (var i = 0; i < febregdays.length; i++){
                                                     win.r2.endDay.add('item', febregdays[i]);}
                                                 }
                                              }
                              win.r2.endDay = add("dropdownlist");
                              win.r2.endDay.minimumSize = [50,20];
                              win.r2.endDay.enabled = false;
                      }
                      win.r3 = add( "group", undefined, undefined );
                          with(win.r3){
                                              var Exit = 0;
                              win.r3.OK = add( "button", undefined, 'OK' );
                              win.r3.Cancel = add( "button", undefined, 'Cancel' );
                                              win.r3.Cancel.onClick = function()
                                              {
                                      win.close();
                                      Exit = 1;                           
                                              }
                              }
                          }
                      win.center();
                      win.show();
                          if(Exit ==1)
                      {
                              this.exit();
                          }
                      Date.prototype.addDays = function(days) {
                          var dat = new Date(this.valueOf())
                          dat.setDate(dat.getDate() + days);
                          return dat;
                      }
                                                  var startfinal = (String(win.r1.startDay.selection + 1)) + " " + (String(win.r1.startMonth.selection)) + " " + (String(win.r1.startValue.text));
                                                  startResult = new Date(startfinal);
                                                  startDate = startResult;
                                                  var endfinal = (String(win.r2.endDay.selection + 1)) + " " + (String(win.r2.endMonth.selection)) + " " + (String(win.r2.endValue.text));
                                                  var endResult = new Date(endfinal);
                                                  stopDate = endResult;
                          var dateArray = new Array();
                      var myDay = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");  
                      var dotw = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31")
                      var myMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");  
                      var d=[];
                      var e=[];
                      var f=[];
                      var g=[];
                          var currentDate = startDate;
                          while (currentDate <= stopDate) {
                              dateArray.push( new Date (currentDate) )
                              var myLetterDateCaller = currentDate.getDay();
                              var myLetterDate = myDay[myLetterDateCaller];
                              var myGoodDate = currentDate.getDate();
                              var myMonthDateCaller = currentDate.getMonth();
                              var myMonthDate = myMonth[myMonthDateCaller];
                              d.push(myGoodDate);
                              e.push(myLetterDate);
                              f.push(myMonthDate);
                                currentDate = currentDate.addDays(1);
                        }
                      var bkp = app.marginPreferences.properties;  
                      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                      app.marginPreferences.properties = {  
                           top:10, left:10, bottom:10, right:10,  
                           columnGutter:0, columnCount:1  
                           };  
                      var myDoc = app.documents.add(true, undefined,  
                           {documentPreferences: {pageWidth:'210mm', pageHeight:'297mm', facingPages: false, createPrimaryTextFrame: true}}  
                           );  
                      app.marginPreferences.properties = bkp;  
                      var myTextFrame = myDoc.pages[0].textFrames[0];  
                      var myText = myTextFrame.texts[0]; 
                      myText.pointSize = 12;
                      myText.leading = 0;
                      for (i = 0; i < d.length; i++){
                      var textReport = e[i] + ", " +d[i] +" " +f[i]+"\r";
                      myTextFrame.contents += textReport;
                       };
                      myTextFrame.parentStory.paragraphs.everyItem().leading = Leading.AUTO;
                      myTextFrame.parentStory.paragraphs.everyItem().startParagraph = StartParagraph.NEXT_FRAME;
                      app.documentPresets[0].createPrimaryTextFrame = storedPreset;  
                      
                      
                      2 people found this helpful