Moment JS start and end of given month

前端 未结 11 1412
无人及你
无人及你 2020-11-28 08:19

I need to calculate a JS date given year=2014 and month=9 (September 2014).

I tried this:

var moment = require(\'moment\');
var startDate = moment( y         


        
相关标签:
11条回答
  • 2020-11-28 08:51

    The following code should work:

    $('#reportrange').daterangepicker({
                    startDate: start,
                    endDate: end,
                    ranges: {
                        'Hoy': [moment(), moment()],
                        'Ayer': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                        'Ultimos 7 dias': [moment().subtract(6, 'days'), moment()],
                        'Ultimos 30 dias': [moment().subtract(29, 'days'), moment()],
                        'Mes actual': [moment().startOf('month'), moment().endOf('month')],
                        'Ultimo mes': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
                        'Enero': [moment().month(0).startOf('month') , moment().month(0).endOf('month')],
                        'Febrero': [moment().month(1).startOf('month') , moment().month(1).endOf('month')],
                        'Marzo': [moment().month(2).startOf('month') , moment().month(2).endOf('month')],
                        'Abril': [moment().month(3).startOf('month') , moment().month(3).endOf('month')],
                        'Mayo': [moment().month(4).startOf('month') , moment().month(4).endOf('month')],
                        'Junio': [moment().month(5).startOf('month') , moment().month(5).endOf('month')],
                        'Julio': [moment().month(6).startOf('month') , moment().month(6).endOf('month')],
                        'Agosto': [moment().month(7).startOf('month') , moment().month(7).endOf('month')],
                        'Septiembre': [moment().month(8).startOf('month') , moment().month(8).endOf('month')],
                        'Octubre': [moment().month(9).startOf('month') , moment().month(9).endOf('month')],
                        'Noviembre': [moment().month(10).startOf('month') , moment().month(10).endOf('month')],
                        'Diciembre': [moment().month(11).startOf('month') , moment().month(11).endOf('month')]
                    }
                }, cb);
    
    0 讨论(0)
  • 2020-11-28 08:53

    you can use this directly for the end or start date of the month

    new moment().startOf('month').format("YYYY-DD-MM");
    new moment().endOf("month").format("YYYY-DD-MM");
    

    you can change the format by defining a new format

    0 讨论(0)
  • 2020-11-28 08:57

    Don't really think there is some direct method to get the last day but you could do something like this:

    var dateInst = new moment();
    /**
     * adding 1 month from the present month and then subtracting 1 day, 
     * So you would get the last day of this month 
     */
    dateInst.add(1, 'months').date(1).subtract(1, 'days');
    /* printing the last day of this month's date */
    console.log(dateInst.format('YYYY MM DD'));
    
    0 讨论(0)
  • 2020-11-28 08:58

    your startDate is first-day-of-month, In this case we can use

    var endDate = moment(startDate).add(1, 'months').subtract(1, 'days');
    

    Hope this helps!!

    0 讨论(0)
  • 2020-11-28 08:58

    Try the following code:

    moment(startDate).startOf('months')
    moment(startDate).endOf('months')
    
    0 讨论(0)
  • 2020-11-28 09:00

    When you use .endOf() you are mutating the object it's called on, so startDate becomes Sep 30

    You should use .clone() to make a copy of it instead of changing it

    var startDate = moment(year + '-' + month + '-' + 01 + ' 00:00:00');
                var endDate = startDate.clone().endOf('month');
                console.log(startDate.toDate());
                console.log(endDate.toDate());
    
    Mon Sep 01 2014 00:00:00 GMT+0700 (ICT) 
    Tue Sep 30 2014 23:59:59 GMT+0700 (ICT) 
    
    0 讨论(0)
提交回复
热议问题