Skip to content
Advertisement

js Daterange Picker time picker showing current time in time range

I want to show time from 00:00 to 23:59 in custom range option for time picker. By default custom range is selected for me. It works with predefined ranges but not with custom range. I couldn’t find any option in docs to set time picker range for custom range time picker. Here is my initialization code.

            var start = {!! isset($_GET['from'])?"moment('".$_GET['from']."')":"moment().startOf('today')"  !!};
            var end = {!! isset($_GET['to'])?"moment('".$_GET['to']."')":"moment().endOf('today')"  !!};

            function cb(s, e) {
                start = s;
                end = e;
                $('#reportrange span').html(start.format('MMM D') + ' - ' + end.format('MMM D'));
            }

               $('#reportrange').daterangepicker({
                timePicker: true,
                timePicker24Hour: true,
                startDate: start,
                endDate: end,
                ranges: {
                    'Today': [moment().startOf('day'), moment().endOf('day')],
                    'Yesterday': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')],
                    'Last 7 Days': [moment().subtract(6, 'days').startOf('day'), moment().endOf('day')],
                    'Last 30 Days': [moment().subtract(29, 'days').startOf('day'), moment().endOf('day')],
                    'This Month': [moment().startOf('month').startOf('day'), moment().endOf('month').endOf('day')],
                    'Last Month': [moment().subtract(1, 'month').startOf('month').startOf('day'), moment().subtract(1, 'month').endOf('month').endOf('day')]
                }
            }, cb);

Advertisement

Answer

You just need to change moment startOf from today to day. Currently its not getting date so it selects custom range by default.

You can look in to moment docs

https://momentjs.com/docs/#/manipulating/start-of/

var start = {!! isset($_GET['from'])?"moment('".$_GET['from']."')":"moment().startOf('day')"  !!};
var end = {!! isset($_GET['to'])?"moment('".$_GET['to']."')":"moment().endOf('day')"  !!};
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement