$("input[name='MyDaterange']").daterangepicker({ ranges: { 'This Week': [moment().day("Sunday"), moment().day("Saturday")], 'This Month': [moment().startOf('month'), moment().endOf('month')], 'This Year': [moment().startOf('year'), moment().endOf('year')], 'Year to Date': [moment().startOf('year'), moment()] }, maxDate: moment() // this is required });
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" /> <input type="text" name="MyDaterange" id="MyDaterange" value="" />
I am using daterangepicker for selecting predefined dates. When I select Year To Date another option This Year gets highlighted.
Code:
$("input[name='MyDaterange']").daterangepicker({
ranges: {
'This Week': [moment().day("Sunday"), moment().day("Saturday")],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'This Year': [moment().startOf('year'), moment().endOf('year')],
'Year to Date': [moment().startOf('year'), moment()]
},
maxDate: moment() // this is required
});
Since you added maxDate: moment()
it means, all dates after today is not counted, it means:
They are the same, and since the plugin check the start and the end date to check for activated range, 'This year' would always the first activated.
Solution:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.