0

I'm working on moment.js and I confused to convert the two dates.

For Example:

    var start = moment().startOf('month');  // Dec 01 2017
    var end = moment(); // Dec 31 2017

I would like to convert this date to last month and last year.

Eg: 
    Last Month: Nov 01 2017 & Nov 30 2017 (Problem in converting 31 to 30)
    Last Year: Dec 01 2016 & Dec 31 2016

If I change the start and end date another value need to change.

Updated:

$(function() {

    var start = moment().startOf('month'); // eg: 12/01/2017
    var end = moment();  // eg: 12/31/2017

    function cb(start, end) {

        var startyear = moment().subtract(start, 'year'); // eg: 12/01/2016
        var endyear = moment().subtract(end, 'year'); // eg: 12/31/2016

        var startmonth = moment().subtract(start, 'month'); // eg: 11/01/2017
        var endmonth = moment().subtract(end, 'month'); // eg: 11/30/2017

        $('#reportrange span').html(start.format('MMM D, YYYY') + ' - ' + end.format('MMM D, YYYY'));
        $('#report1 span').html(startyear.format('MMM D, YYYY') + ' - ' + endyear.format('MMM D, YYYY'));
        $('#report2 span').html(startmonth.format('MMM D, YYYY') + ' - ' + endmonth.format('MMM D, YYYY'));
    }

     $('#reportrange').daterangepicker({
         "autoApply": true,
        "showDropdowns": false,
        "alwaysShowCalendars": false,
        "opens": "left",
        startDate: start,
        endDate: end,
    },cb);  

    cb(start, end);
    });

If I select two dates, I want to convert those dates to year and month.

Sarath
  • 37
  • 1
  • 1
  • 8

2 Answers2

0

I think your problem is you are trying to decrease the month and year but not checking the last day in the month.

Check this example:

var myDate = new Date(2017, 11, 31);
console.log(myDate);

Sun Dec 31 2017 00:00:00 GMT-0200 (E. South America Daylight Time)

If you try to decrease the year and month and not check the last day of the month will be one day more changing the month again:

myDate.setFullYear(2016, 10, 31);
console.log(myDate);

Thu Dec 01 2016 00:00:00 GMT-0200 (E. South America Daylight Time)

If you decrease checking the day to not pass the max will work fine:

myDate.setFullYear(2016, 10, 30);
console.log(myDate);

Wed Nov 30 2016 00:00:00 GMT-0200 (E. South America Daylight Time)

Is already a question to how calculate the last day of month here Calculate last day of month in javascript

Ciro Spaciari
  • 630
  • 5
  • 10
0

If you are looking for start & end date of last month and year based on current date, you can simply do:

var yourDate = '2017-11-12';

var startOfLastMonth = moment(yourDate).subtract(1, 'month').startOf('month');
var endOfLastMonth = moment(yourDate).subtract(1, 'month').endOf('month');

var startOfLastYear = moment(yourDate).subtract(1, 'year').startOf('month');
var endOfLastYear = moment(yourDate).subtract(1, 'year').endOf('month');

console.log('Your Date: ' + moment(yourDate).format('MMM DD YYYY'))
console.log('')
console.log(startOfLastMonth.format('MMM DD YYYY'))
console.log(endOfLastMonth.format('MMM DD YYYY'))
console.log('')
console.log(startOfLastYear.format('MMM DD YYYY'))
console.log(endOfLastYear.format('MMM DD YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Hi @palasH you are correct but I want to convert the date based on my selection. For example, if I choose the date Nov 1, 2017 and Nov 12, 2017 that only convert to month as well year. – Sarath Dec 22 '17 at 14:43
  • Just pass your date in to moment in YYYY-MM-DD format like `moment('2016-01-01')` and same logic will work fine. Code updated! – palaѕн Dec 22 '17 at 14:47