如何使用moment.js将选定的日期转换为上个月和去年的日期?

时间:2022-05-27 04:34:35

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

我正在研究moment.js,我很困惑地转换这两个日期。

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.

如果我选择两个日期,我想将这些日期转换为年和月。

2 个解决方案

#1


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>

#2


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)

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)

2016年12月1日星期四00:00:00 GMT-0200(南美洲夏令时)

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)

2016年11月30日星期三00:00:00 GMT-0200(南美洲夏令时)

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

如何计算这里的最后几天已经是一个问题在javascript中计算月份的最后一天

#1


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>

#2


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)

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)

2016年12月1日星期四00:00:00 GMT-0200(南美洲夏令时)

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)

2016年11月30日星期三00:00:00 GMT-0200(南美洲夏令时)

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

如何计算这里的最后几天已经是一个问题在javascript中计算月份的最后一天