Get the day number from moment object javascript

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.



What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?



var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];


days = sum - (month_day[mon-1] - day);
console.log(days)









share|improve this question























  • I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
    – Nimesha Kalinga
    Sep 10 at 3:51






  • 3




    Not only brute force, but also incorrect because it ignores leap years.
    – Robby Cornelissen
    Sep 10 at 3:52














up vote
6
down vote

favorite
1












I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.



What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?



var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];


days = sum - (month_day[mon-1] - day);
console.log(days)









share|improve this question























  • I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
    – Nimesha Kalinga
    Sep 10 at 3:51






  • 3




    Not only brute force, but also incorrect because it ignores leap years.
    – Robby Cornelissen
    Sep 10 at 3:52












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.



What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?



var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];


days = sum - (month_day[mon-1] - day);
console.log(days)









share|improve this question















I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.



What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?



var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];


days = sum - (month_day[mon-1] - day);
console.log(days)






javascript momentjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 10 at 9:45









Sumurai8

12.2k73059




12.2k73059










asked Sep 10 at 3:50









Nimesha Kalinga

1058




1058











  • I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
    – Nimesha Kalinga
    Sep 10 at 3:51






  • 3




    Not only brute force, but also incorrect because it ignores leap years.
    – Robby Cornelissen
    Sep 10 at 3:52
















  • I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
    – Nimesha Kalinga
    Sep 10 at 3:51






  • 3




    Not only brute force, but also incorrect because it ignores leap years.
    – Robby Cornelissen
    Sep 10 at 3:52















I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
– Nimesha Kalinga
Sep 10 at 3:51




I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
– Nimesha Kalinga
Sep 10 at 3:51




3




3




Not only brute force, but also incorrect because it ignores leap years.
– Robby Cornelissen
Sep 10 at 3:52




Not only brute force, but also incorrect because it ignores leap years.
– Robby Cornelissen
Sep 10 at 3:52












3 Answers
3






active

oldest

votes

















up vote
13
down vote



accepted










You can use the dayOfYear() function:






const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);

console.log(date.dayOfYear()); // 359

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








share|improve this answer





























    up vote
    3
    down vote













    You can do it without momentjs






    let year = 2018;
    let month = 12 - 1;
    let day = 25;
    let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
    console.log(dayOfYear);








    share|improve this answer


















    • 1




      Info: momentjs use a somewhat similar approach.
      – user202729
      Sep 10 at 11:30

















    up vote
    0
    down vote













    https://momentjs.com/docs/#/get-set/day-of-year/



    var day = 25;
    var mon = 12;
    var year = 2018;
    console.log(moment().year(year).month(mon).date(day).dayOfYear());


    You should check the docs before asking questions on Stack Overflow.






    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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f52250848%2fget-the-day-number-from-moment-object-javascript%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote



      accepted










      You can use the dayOfYear() function:






      const day = 25;
      const month = 12 - 1; // months are 0-based when using the object constructor
      const year = 2018;
      const date = moment(day, month, year);

      console.log(date.dayOfYear()); // 359

      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








      share|improve this answer


























        up vote
        13
        down vote



        accepted










        You can use the dayOfYear() function:






        const day = 25;
        const month = 12 - 1; // months are 0-based when using the object constructor
        const year = 2018;
        const date = moment(day, month, year);

        console.log(date.dayOfYear()); // 359

        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








        share|improve this answer
























          up vote
          13
          down vote



          accepted







          up vote
          13
          down vote



          accepted






          You can use the dayOfYear() function:






          const day = 25;
          const month = 12 - 1; // months are 0-based when using the object constructor
          const year = 2018;
          const date = moment(day, month, year);

          console.log(date.dayOfYear()); // 359

          <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








          share|improve this answer














          You can use the dayOfYear() function:






          const day = 25;
          const month = 12 - 1; // months are 0-based when using the object constructor
          const year = 2018;
          const date = moment(day, month, year);

          console.log(date.dayOfYear()); // 359

          <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








          const day = 25;
          const month = 12 - 1; // months are 0-based when using the object constructor
          const year = 2018;
          const date = moment(day, month, year);

          console.log(date.dayOfYear()); // 359

          <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>





          const day = 25;
          const month = 12 - 1; // months are 0-based when using the object constructor
          const year = 2018;
          const date = moment(day, month, year);

          console.log(date.dayOfYear()); // 359

          <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 10 at 4:03









          VicJordan

          10.1k74069




          10.1k74069










          answered Sep 10 at 4:00









          Robby Cornelissen

          40.1k126488




          40.1k126488






















              up vote
              3
              down vote













              You can do it without momentjs






              let year = 2018;
              let month = 12 - 1;
              let day = 25;
              let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
              console.log(dayOfYear);








              share|improve this answer


















              • 1




                Info: momentjs use a somewhat similar approach.
                – user202729
                Sep 10 at 11:30














              up vote
              3
              down vote













              You can do it without momentjs






              let year = 2018;
              let month = 12 - 1;
              let day = 25;
              let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
              console.log(dayOfYear);








              share|improve this answer


















              • 1




                Info: momentjs use a somewhat similar approach.
                – user202729
                Sep 10 at 11:30












              up vote
              3
              down vote










              up vote
              3
              down vote









              You can do it without momentjs






              let year = 2018;
              let month = 12 - 1;
              let day = 25;
              let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
              console.log(dayOfYear);








              share|improve this answer














              You can do it without momentjs






              let year = 2018;
              let month = 12 - 1;
              let day = 25;
              let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
              console.log(dayOfYear);








              let year = 2018;
              let month = 12 - 1;
              let day = 25;
              let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
              console.log(dayOfYear);





              let year = 2018;
              let month = 12 - 1;
              let day = 25;
              let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
              console.log(dayOfYear);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 10 at 4:35

























              answered Sep 10 at 4:08









              Dmitry Kolchev

              77435




              77435







              • 1




                Info: momentjs use a somewhat similar approach.
                – user202729
                Sep 10 at 11:30












              • 1




                Info: momentjs use a somewhat similar approach.
                – user202729
                Sep 10 at 11:30







              1




              1




              Info: momentjs use a somewhat similar approach.
              – user202729
              Sep 10 at 11:30




              Info: momentjs use a somewhat similar approach.
              – user202729
              Sep 10 at 11:30










              up vote
              0
              down vote













              https://momentjs.com/docs/#/get-set/day-of-year/



              var day = 25;
              var mon = 12;
              var year = 2018;
              console.log(moment().year(year).month(mon).date(day).dayOfYear());


              You should check the docs before asking questions on Stack Overflow.






              share|improve this answer
























                up vote
                0
                down vote













                https://momentjs.com/docs/#/get-set/day-of-year/



                var day = 25;
                var mon = 12;
                var year = 2018;
                console.log(moment().year(year).month(mon).date(day).dayOfYear());


                You should check the docs before asking questions on Stack Overflow.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  https://momentjs.com/docs/#/get-set/day-of-year/



                  var day = 25;
                  var mon = 12;
                  var year = 2018;
                  console.log(moment().year(year).month(mon).date(day).dayOfYear());


                  You should check the docs before asking questions on Stack Overflow.






                  share|improve this answer












                  https://momentjs.com/docs/#/get-set/day-of-year/



                  var day = 25;
                  var mon = 12;
                  var year = 2018;
                  console.log(moment().year(year).month(mon).date(day).dayOfYear());


                  You should check the docs before asking questions on Stack Overflow.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 10 at 3:58









                  DrevanTonder

                  324617




                  324617



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52250848%2fget-the-day-number-from-moment-object-javascript%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      這個網誌中的熱門文章

                      tkz-euclide: tkzDrawCircle[R] not working

                      How to combine Bézier curves to a surface?

                      1st Magritte Awards