How to Extract Months from dates contains in a html table column?












0














I have a html table. One column contains date values. I want to extract month of those dates and print it in the console. Date format is yyyy-MM-dd. Here is my code:



var myTable = document.getElementById('openDispatchNoteTable');

for (var i = 0 ; i < myTable.rows.length ; i++) {
console.log("date : " + myTable.rows[i].cells[6].innerText);
}


myTable.rows[i].cells[6].innerText gives 2018-November-14 as the output.



Can you please help me to solve this problem?










share|improve this question





























    0














    I have a html table. One column contains date values. I want to extract month of those dates and print it in the console. Date format is yyyy-MM-dd. Here is my code:



    var myTable = document.getElementById('openDispatchNoteTable');

    for (var i = 0 ; i < myTable.rows.length ; i++) {
    console.log("date : " + myTable.rows[i].cells[6].innerText);
    }


    myTable.rows[i].cells[6].innerText gives 2018-November-14 as the output.



    Can you please help me to solve this problem?










    share|improve this question



























      0












      0








      0







      I have a html table. One column contains date values. I want to extract month of those dates and print it in the console. Date format is yyyy-MM-dd. Here is my code:



      var myTable = document.getElementById('openDispatchNoteTable');

      for (var i = 0 ; i < myTable.rows.length ; i++) {
      console.log("date : " + myTable.rows[i].cells[6].innerText);
      }


      myTable.rows[i].cells[6].innerText gives 2018-November-14 as the output.



      Can you please help me to solve this problem?










      share|improve this question















      I have a html table. One column contains date values. I want to extract month of those dates and print it in the console. Date format is yyyy-MM-dd. Here is my code:



      var myTable = document.getElementById('openDispatchNoteTable');

      for (var i = 0 ; i < myTable.rows.length ; i++) {
      console.log("date : " + myTable.rows[i].cells[6].innerText);
      }


      myTable.rows[i].cells[6].innerText gives 2018-November-14 as the output.



      Can you please help me to solve this problem?







      javascript html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 3:00

























      asked Nov 20 at 10:55









      Ravindu Saluwadana

      117




      117
























          3 Answers
          3






          active

          oldest

          votes


















          2














          if format is the same in all of outputs - then use regexp .innerText.match(/[a-z]+/i)[0]






          console.log(document.querySelector('div').innerText.match(/[a-z]+/i)[0])

          <div>2018-November-14</div>





          Also i found good internationalized solution here: get month name from date Second best answer uses toLocaleString.






          share|improve this answer























          • Thank you very much for your answer. This worked for me.
            – Ravindu Saluwadana
            Nov 20 at 11:09



















          1














          In your case you can use the replace() method of Javascript.



          Use this replace(/[^a-zA-Z ]/g, "") regex which will replace everything with "" other then characters.






          var myTable = document.getElementById("myTable");
          for (var i = 1; i < myTable.rows.length; i++) {
          console.log("Actual Date : " + myTable.rows[i].cells[0].innerText);
          console.log("Only Month : " + myTable.rows[i].cells[0].innerText.replace(/[^a-zA-Z ]/g, ""));
          }

          <table id="myTable" border='1'>
          <tr>
          <th>Date</th>
          </tr>
          <tr>
          <td>
          28-November-2018
          </td>
          </tr>
          <tr>
          <td>
          29-November-2018
          </td>
          </tr>
          <tr>
          <td>
          30-November-2018
          </td>
          </tr>
          </table>








          share|improve this answer































            0














            I highly recommend using momentJS. It has a very elaborate API to handle things just like your need. In this case, the moment object can get the date in your format and then you can use the month function to extract the month.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391453%2fhow-to-extract-months-from-dates-contains-in-a-html-table-column%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              if format is the same in all of outputs - then use regexp .innerText.match(/[a-z]+/i)[0]






              console.log(document.querySelector('div').innerText.match(/[a-z]+/i)[0])

              <div>2018-November-14</div>





              Also i found good internationalized solution here: get month name from date Second best answer uses toLocaleString.






              share|improve this answer























              • Thank you very much for your answer. This worked for me.
                – Ravindu Saluwadana
                Nov 20 at 11:09
















              2














              if format is the same in all of outputs - then use regexp .innerText.match(/[a-z]+/i)[0]






              console.log(document.querySelector('div').innerText.match(/[a-z]+/i)[0])

              <div>2018-November-14</div>





              Also i found good internationalized solution here: get month name from date Second best answer uses toLocaleString.






              share|improve this answer























              • Thank you very much for your answer. This worked for me.
                – Ravindu Saluwadana
                Nov 20 at 11:09














              2












              2








              2






              if format is the same in all of outputs - then use regexp .innerText.match(/[a-z]+/i)[0]






              console.log(document.querySelector('div').innerText.match(/[a-z]+/i)[0])

              <div>2018-November-14</div>





              Also i found good internationalized solution here: get month name from date Second best answer uses toLocaleString.






              share|improve this answer














              if format is the same in all of outputs - then use regexp .innerText.match(/[a-z]+/i)[0]






              console.log(document.querySelector('div').innerText.match(/[a-z]+/i)[0])

              <div>2018-November-14</div>





              Also i found good internationalized solution here: get month name from date Second best answer uses toLocaleString.






              console.log(document.querySelector('div').innerText.match(/[a-z]+/i)[0])

              <div>2018-November-14</div>





              console.log(document.querySelector('div').innerText.match(/[a-z]+/i)[0])

              <div>2018-November-14</div>






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 20 at 11:10

























              answered Nov 20 at 11:01









              Smollet777

              1,3461015




              1,3461015












              • Thank you very much for your answer. This worked for me.
                – Ravindu Saluwadana
                Nov 20 at 11:09


















              • Thank you very much for your answer. This worked for me.
                – Ravindu Saluwadana
                Nov 20 at 11:09
















              Thank you very much for your answer. This worked for me.
              – Ravindu Saluwadana
              Nov 20 at 11:09




              Thank you very much for your answer. This worked for me.
              – Ravindu Saluwadana
              Nov 20 at 11:09













              1














              In your case you can use the replace() method of Javascript.



              Use this replace(/[^a-zA-Z ]/g, "") regex which will replace everything with "" other then characters.






              var myTable = document.getElementById("myTable");
              for (var i = 1; i < myTable.rows.length; i++) {
              console.log("Actual Date : " + myTable.rows[i].cells[0].innerText);
              console.log("Only Month : " + myTable.rows[i].cells[0].innerText.replace(/[^a-zA-Z ]/g, ""));
              }

              <table id="myTable" border='1'>
              <tr>
              <th>Date</th>
              </tr>
              <tr>
              <td>
              28-November-2018
              </td>
              </tr>
              <tr>
              <td>
              29-November-2018
              </td>
              </tr>
              <tr>
              <td>
              30-November-2018
              </td>
              </tr>
              </table>








              share|improve this answer




























                1














                In your case you can use the replace() method of Javascript.



                Use this replace(/[^a-zA-Z ]/g, "") regex which will replace everything with "" other then characters.






                var myTable = document.getElementById("myTable");
                for (var i = 1; i < myTable.rows.length; i++) {
                console.log("Actual Date : " + myTable.rows[i].cells[0].innerText);
                console.log("Only Month : " + myTable.rows[i].cells[0].innerText.replace(/[^a-zA-Z ]/g, ""));
                }

                <table id="myTable" border='1'>
                <tr>
                <th>Date</th>
                </tr>
                <tr>
                <td>
                28-November-2018
                </td>
                </tr>
                <tr>
                <td>
                29-November-2018
                </td>
                </tr>
                <tr>
                <td>
                30-November-2018
                </td>
                </tr>
                </table>








                share|improve this answer


























                  1












                  1








                  1






                  In your case you can use the replace() method of Javascript.



                  Use this replace(/[^a-zA-Z ]/g, "") regex which will replace everything with "" other then characters.






                  var myTable = document.getElementById("myTable");
                  for (var i = 1; i < myTable.rows.length; i++) {
                  console.log("Actual Date : " + myTable.rows[i].cells[0].innerText);
                  console.log("Only Month : " + myTable.rows[i].cells[0].innerText.replace(/[^a-zA-Z ]/g, ""));
                  }

                  <table id="myTable" border='1'>
                  <tr>
                  <th>Date</th>
                  </tr>
                  <tr>
                  <td>
                  28-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  29-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  30-November-2018
                  </td>
                  </tr>
                  </table>








                  share|improve this answer














                  In your case you can use the replace() method of Javascript.



                  Use this replace(/[^a-zA-Z ]/g, "") regex which will replace everything with "" other then characters.






                  var myTable = document.getElementById("myTable");
                  for (var i = 1; i < myTable.rows.length; i++) {
                  console.log("Actual Date : " + myTable.rows[i].cells[0].innerText);
                  console.log("Only Month : " + myTable.rows[i].cells[0].innerText.replace(/[^a-zA-Z ]/g, ""));
                  }

                  <table id="myTable" border='1'>
                  <tr>
                  <th>Date</th>
                  </tr>
                  <tr>
                  <td>
                  28-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  29-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  30-November-2018
                  </td>
                  </tr>
                  </table>








                  var myTable = document.getElementById("myTable");
                  for (var i = 1; i < myTable.rows.length; i++) {
                  console.log("Actual Date : " + myTable.rows[i].cells[0].innerText);
                  console.log("Only Month : " + myTable.rows[i].cells[0].innerText.replace(/[^a-zA-Z ]/g, ""));
                  }

                  <table id="myTable" border='1'>
                  <tr>
                  <th>Date</th>
                  </tr>
                  <tr>
                  <td>
                  28-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  29-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  30-November-2018
                  </td>
                  </tr>
                  </table>





                  var myTable = document.getElementById("myTable");
                  for (var i = 1; i < myTable.rows.length; i++) {
                  console.log("Actual Date : " + myTable.rows[i].cells[0].innerText);
                  console.log("Only Month : " + myTable.rows[i].cells[0].innerText.replace(/[^a-zA-Z ]/g, ""));
                  }

                  <table id="myTable" border='1'>
                  <tr>
                  <th>Date</th>
                  </tr>
                  <tr>
                  <td>
                  28-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  29-November-2018
                  </td>
                  </tr>
                  <tr>
                  <td>
                  30-November-2018
                  </td>
                  </tr>
                  </table>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 11:21









                  Smollet777

                  1,3461015




                  1,3461015










                  answered Nov 20 at 11:16









                  Mohan Rajput

                  27312




                  27312























                      0














                      I highly recommend using momentJS. It has a very elaborate API to handle things just like your need. In this case, the moment object can get the date in your format and then you can use the month function to extract the month.






                      share|improve this answer


























                        0














                        I highly recommend using momentJS. It has a very elaborate API to handle things just like your need. In this case, the moment object can get the date in your format and then you can use the month function to extract the month.






                        share|improve this answer
























                          0












                          0








                          0






                          I highly recommend using momentJS. It has a very elaborate API to handle things just like your need. In this case, the moment object can get the date in your format and then you can use the month function to extract the month.






                          share|improve this answer












                          I highly recommend using momentJS. It has a very elaborate API to handle things just like your need. In this case, the moment object can get the date in your format and then you can use the month function to extract the month.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 11:01









                          Gilad Bar

                          633314




                          633314






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391453%2fhow-to-extract-months-from-dates-contains-in-a-html-table-column%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Create new schema in PostgreSQL using DBeaver

                              Deepest pit of an array with Javascript: test on Codility

                              Fotorealismo