简体   繁体   中英

How to convert the selected date to last month and last year date using moment.js?

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.

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

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> 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM