bootstrap-datetimepicker

Set to Monday of selected week on change


I am using bootstrap datetimepicker for my date select field which displays week Mon - Sun. When a user clicks on any date in the week, I need it to set Monday as the date. The below code works great except when the user clicks on Sunday, it selects Monday from the following week.

$('#start_date').on('dp.change', function (e) {
            var value = $("#start_date").val();
            var firstDate = moment(value, "YYYY-MM-DD").day(1).format("YYYY-MM-DD");
            
            $("#start_date").val(firstDate);
        });

How can I have the date set to Monday of the week displayed when Sun is selected.


Solution

  • Found a fix that works by setting the start of week to 0 in moment.js

    var firstDate = moment(value, "YYYY-MM-DD").startOf('week').isoWeekday(0).day(1).format("YYYY-MM-DD");