Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have problem with showing current financial year and last financial year in bootstrap date range picker. i need this financial year to be from April 2016 to March 2017. But presently this financial year showing April 2017- March 2018. But January 2017 coming under Last financial year on selection.

JavaScript
//Date range as a button
   $('.daterange-btn').daterangepicker({
       ranges: {
           'Today': [moment(), moment()],
           'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
           'Last 7 Days': [moment().subtract(6, 'days'), moment()],
           'Last 30 Days': [moment().subtract(29, 'days'), moment()],
           'This Month': [moment().startOf('month'), moment().endOf('month')],
           'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
           'This Financial Year': [moment().month(new Date(startDate).getMonth()).startOf('month'), moment().month(new Date(startDate).getMonth()).startOf('month').subtract(1, 'days').add(1, 'year')],
           'Last Financial Year': [moment().month(3).startOf('month').subtract(1, 'year'), moment().month(3).startOf('month').subtract(1, 'days')]

       },
       startDate: moment().subtract(29, 'days'),
       endDate: moment()
   }, function (start, end) {
       Search(start.format('DD-MMM-YYYY'), end.format('DD-MMM-YYYY'));
   });


What I have tried:

I am unable to solve this please help
Posted
Updated 16-Jan-17 4:50am
v2

why don't you try jquery datepicker.Here,we can provide year and month range
 
Share this answer
 
Hardly surprising, given your range definitions:
JavaScript
'This Financial Year': 
[
    moment() // 2017-01-16
        .month(new Date(startDate).getMonth()) // 2017-04-16
        .startOf('month'), // 2017-04-01
    
    moment() // 2017-01-17
        .month(new Date(startDate).getMonth()) // 2017-04-16
        .startOf('month') // 2017-04-01
        .subtract(1, 'days') // 2017-03-31
        .add(1, 'year') // 2018-03-31
],
'Last Financial Year': 
[
    moment() // 2017-01-16
        .month(3) // 2017-04-16
        .startOf('month') // 2017-04-01
        .subtract(1, 'year'), // 2016-04-01
    
    moment() // 2017-01-16
        .month(3) // 2017-04-16
        .startOf('month') // 2017-04-01
        .subtract(1, 'days') // 2017-03-31
]

You need to check whether the current month is before the start of the financial year. If it is, then both ranges shift back a year.
JavaScript
var currentMonth = new Date().getMonth();
var financialYearStartMonth = new Date(startDate).getMonth();
var financialYearStartDate = moment().month(financialYearStartMonth).startOf('month');
if (currentMonth < financialYearStartMonth) {
    financialYearStartDate = financialYearStartDate.subtract(1, 'year');
}

$('.daterange-btn').daterangepicker({
    ranges: {
        ...
        'This Financial Year': [financialYearStartDate, financialYearStartDate.add(1, 'year').subtract(1, 'days')],
        'Last Financial Year': [financialYearStartDate.subtract(1, 'year'), financialYearStartDate.subtract(1, 'days')]
    },
    ...
 
Share this answer
 
Comments
Member 10212775 17-Jan-17 4:07am    
thank you very much for the idea. i changed code to

var thisStart = moment() // 2017-01-16
.month(new Date(startDate).getMonth()) // 2017-04-16
.startOf('month');

var thisEnd = moment() // 2017-01-17
.month(new Date(startDate).getMonth()) // 2017-04-16
.startOf('month') // 2017-04-01
.subtract(1, 'days') // 2017-03-31
.add(1, 'year');


if (currentMonth < financialYearStartMonth) {
thisStart = thisStart.subtract(1, 'year');
thisEnd = thisEnd.subtract(1, 'year');
}



//Date range as a button
$('.daterange-btn').daterangepicker({
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
'This Financial Year':[thisStart,thisEnd]

},

startDate: moment().subtract(29, 'days'),
endDate: moment()
},
function (start, end) {
Search(start.format('DD-MMM-YYYY'), end.format('DD-MMM-YYYY'));
});

and its working fine now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900