How to get first date of the week based on week number and year in Moment.js?












1















I tried to get the start date of a specific week in Moment.js from the week number and year by doing moment().year(...).isoWeek(...).startOf('isoWeek')



But It seems that this function is not always returning the correct date.
For example when I live in England and a week always starts on a monday.
We should get 31 Dec 2018 when we ask for the first day of week 1, 2019.



This wasn't the case on 31 Dec 2018 as the result I received was 30 Dec 2019 as the begin date of week 1, 2019. See example










share|improve this question





























    1















    I tried to get the start date of a specific week in Moment.js from the week number and year by doing moment().year(...).isoWeek(...).startOf('isoWeek')



    But It seems that this function is not always returning the correct date.
    For example when I live in England and a week always starts on a monday.
    We should get 31 Dec 2018 when we ask for the first day of week 1, 2019.



    This wasn't the case on 31 Dec 2018 as the result I received was 30 Dec 2019 as the begin date of week 1, 2019. See example










    share|improve this question



























      1












      1








      1








      I tried to get the start date of a specific week in Moment.js from the week number and year by doing moment().year(...).isoWeek(...).startOf('isoWeek')



      But It seems that this function is not always returning the correct date.
      For example when I live in England and a week always starts on a monday.
      We should get 31 Dec 2018 when we ask for the first day of week 1, 2019.



      This wasn't the case on 31 Dec 2018 as the result I received was 30 Dec 2019 as the begin date of week 1, 2019. See example










      share|improve this question
















      I tried to get the start date of a specific week in Moment.js from the week number and year by doing moment().year(...).isoWeek(...).startOf('isoWeek')



      But It seems that this function is not always returning the correct date.
      For example when I live in England and a week always starts on a monday.
      We should get 31 Dec 2018 when we ask for the first day of week 1, 2019.



      This wasn't the case on 31 Dec 2018 as the result I received was 30 Dec 2019 as the begin date of week 1, 2019. See example







      momentjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 10 at 11:27







      Logan_Dupont

















      asked Jan 2 at 14:07









      Logan_DupontLogan_Dupont

      335




      335
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Please note that, as i18n section of the docs states:




          By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.




          So if you want to use en-gb locale you have explicitly load it (in the browser, you can use en-gb.js file or moment-with-locales.js and then set locale using moment.locale('en-gb')).



          You don't have to use year() setter, because it sets the year to 2019 and moment().year(2019).isoWeek(1) gives you the first isoweek of the 2020. You can create a moment object for a given year using moment({y: year}) instead.



          You have to use week() instead of isoWeek if you want locale dependent results:




          Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.



          The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year.




          Here a full code sample:






          // Set locale to British English
          moment.locale('en-gb');
          var year = 2019;
          var firstMonday = moment({y: year}) // get first day of the given year
          .week(1) // get the first week according locale
          .startOf('week'); // get the first day of the week according locale
          // Show result
          console.log(firstMonday.format());

          <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>





          You can use format() to display the value of a moment object.






          share|improve this answer
























          • I was aware that I had to use locale and set it to the correct location. But I wasn't able to get it working in my snippet.

            – Logan_Dupont
            Jan 10 at 11:49











          • For week and year I wanted to use the global numbering as later on we will have different locations in our project. That's why i need to use the isoWeek and isoWeekYear.

            – Logan_Dupont
            Jan 10 at 13:01



















          0














          I think I found the solution I was looking for



          moment()
          .isoWeekYear(year)
          .isoWeek(week)
          .startOf('week')






          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%2f54007784%2fhow-to-get-first-date-of-the-week-based-on-week-number-and-year-in-moment-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Please note that, as i18n section of the docs states:




            By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.




            So if you want to use en-gb locale you have explicitly load it (in the browser, you can use en-gb.js file or moment-with-locales.js and then set locale using moment.locale('en-gb')).



            You don't have to use year() setter, because it sets the year to 2019 and moment().year(2019).isoWeek(1) gives you the first isoweek of the 2020. You can create a moment object for a given year using moment({y: year}) instead.



            You have to use week() instead of isoWeek if you want locale dependent results:




            Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.



            The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year.




            Here a full code sample:






            // Set locale to British English
            moment.locale('en-gb');
            var year = 2019;
            var firstMonday = moment({y: year}) // get first day of the given year
            .week(1) // get the first week according locale
            .startOf('week'); // get the first day of the week according locale
            // Show result
            console.log(firstMonday.format());

            <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>





            You can use format() to display the value of a moment object.






            share|improve this answer
























            • I was aware that I had to use locale and set it to the correct location. But I wasn't able to get it working in my snippet.

              – Logan_Dupont
              Jan 10 at 11:49











            • For week and year I wanted to use the global numbering as later on we will have different locations in our project. That's why i need to use the isoWeek and isoWeekYear.

              – Logan_Dupont
              Jan 10 at 13:01
















            0














            Please note that, as i18n section of the docs states:




            By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.




            So if you want to use en-gb locale you have explicitly load it (in the browser, you can use en-gb.js file or moment-with-locales.js and then set locale using moment.locale('en-gb')).



            You don't have to use year() setter, because it sets the year to 2019 and moment().year(2019).isoWeek(1) gives you the first isoweek of the 2020. You can create a moment object for a given year using moment({y: year}) instead.



            You have to use week() instead of isoWeek if you want locale dependent results:




            Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.



            The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year.




            Here a full code sample:






            // Set locale to British English
            moment.locale('en-gb');
            var year = 2019;
            var firstMonday = moment({y: year}) // get first day of the given year
            .week(1) // get the first week according locale
            .startOf('week'); // get the first day of the week according locale
            // Show result
            console.log(firstMonday.format());

            <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>





            You can use format() to display the value of a moment object.






            share|improve this answer
























            • I was aware that I had to use locale and set it to the correct location. But I wasn't able to get it working in my snippet.

              – Logan_Dupont
              Jan 10 at 11:49











            • For week and year I wanted to use the global numbering as later on we will have different locations in our project. That's why i need to use the isoWeek and isoWeekYear.

              – Logan_Dupont
              Jan 10 at 13:01














            0












            0








            0







            Please note that, as i18n section of the docs states:




            By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.




            So if you want to use en-gb locale you have explicitly load it (in the browser, you can use en-gb.js file or moment-with-locales.js and then set locale using moment.locale('en-gb')).



            You don't have to use year() setter, because it sets the year to 2019 and moment().year(2019).isoWeek(1) gives you the first isoweek of the 2020. You can create a moment object for a given year using moment({y: year}) instead.



            You have to use week() instead of isoWeek if you want locale dependent results:




            Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.



            The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year.




            Here a full code sample:






            // Set locale to British English
            moment.locale('en-gb');
            var year = 2019;
            var firstMonday = moment({y: year}) // get first day of the given year
            .week(1) // get the first week according locale
            .startOf('week'); // get the first day of the week according locale
            // Show result
            console.log(firstMonday.format());

            <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>





            You can use format() to display the value of a moment object.






            share|improve this answer













            Please note that, as i18n section of the docs states:




            By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.




            So if you want to use en-gb locale you have explicitly load it (in the browser, you can use en-gb.js file or moment-with-locales.js and then set locale using moment.locale('en-gb')).



            You don't have to use year() setter, because it sets the year to 2019 and moment().year(2019).isoWeek(1) gives you the first isoweek of the 2020. You can create a moment object for a given year using moment({y: year}) instead.



            You have to use week() instead of isoWeek if you want locale dependent results:




            Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.



            The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year.




            Here a full code sample:






            // Set locale to British English
            moment.locale('en-gb');
            var year = 2019;
            var firstMonday = moment({y: year}) // get first day of the given year
            .week(1) // get the first week according locale
            .startOf('week'); // get the first day of the week according locale
            // Show result
            console.log(firstMonday.format());

            <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>





            You can use format() to display the value of a moment object.






            // Set locale to British English
            moment.locale('en-gb');
            var year = 2019;
            var firstMonday = moment({y: year}) // get first day of the given year
            .week(1) // get the first week according locale
            .startOf('week'); // get the first day of the week according locale
            // Show result
            console.log(firstMonday.format());

            <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>





            // Set locale to British English
            moment.locale('en-gb');
            var year = 2019;
            var firstMonday = moment({y: year}) // get first day of the given year
            .week(1) // get the first week according locale
            .startOf('week'); // get the first day of the week according locale
            // Show result
            console.log(firstMonday.format());

            <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment-with-locales.min.js"></script>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 15:36









            VincenzoCVincenzoC

            16k83957




            16k83957













            • I was aware that I had to use locale and set it to the correct location. But I wasn't able to get it working in my snippet.

              – Logan_Dupont
              Jan 10 at 11:49











            • For week and year I wanted to use the global numbering as later on we will have different locations in our project. That's why i need to use the isoWeek and isoWeekYear.

              – Logan_Dupont
              Jan 10 at 13:01



















            • I was aware that I had to use locale and set it to the correct location. But I wasn't able to get it working in my snippet.

              – Logan_Dupont
              Jan 10 at 11:49











            • For week and year I wanted to use the global numbering as later on we will have different locations in our project. That's why i need to use the isoWeek and isoWeekYear.

              – Logan_Dupont
              Jan 10 at 13:01

















            I was aware that I had to use locale and set it to the correct location. But I wasn't able to get it working in my snippet.

            – Logan_Dupont
            Jan 10 at 11:49





            I was aware that I had to use locale and set it to the correct location. But I wasn't able to get it working in my snippet.

            – Logan_Dupont
            Jan 10 at 11:49













            For week and year I wanted to use the global numbering as later on we will have different locations in our project. That's why i need to use the isoWeek and isoWeekYear.

            – Logan_Dupont
            Jan 10 at 13:01





            For week and year I wanted to use the global numbering as later on we will have different locations in our project. That's why i need to use the isoWeek and isoWeekYear.

            – Logan_Dupont
            Jan 10 at 13:01













            0














            I think I found the solution I was looking for



            moment()
            .isoWeekYear(year)
            .isoWeek(week)
            .startOf('week')






            share|improve this answer




























              0














              I think I found the solution I was looking for



              moment()
              .isoWeekYear(year)
              .isoWeek(week)
              .startOf('week')






              share|improve this answer


























                0












                0








                0







                I think I found the solution I was looking for



                moment()
                .isoWeekYear(year)
                .isoWeek(week)
                .startOf('week')






                share|improve this answer













                I think I found the solution I was looking for



                moment()
                .isoWeekYear(year)
                .isoWeek(week)
                .startOf('week')







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 10 at 11:35









                Logan_DupontLogan_Dupont

                335




                335






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007784%2fhow-to-get-first-date-of-the-week-based-on-week-number-and-year-in-moment-js%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

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    Npm cannot find a required file even through it is in the searched directory