Moment JS start and end of given month

前端 未结 11 1413
无人及你
无人及你 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 09:01

    That's because endOf mutates the original value.

    Relevant quote:

    Mutates the original moment by setting it to the end of a unit of time.

    Here's an example function that gives you the output you want:

    function getMonthDateRange(year, month) {
        var moment = require('moment');
    
        // month in moment is 0 based, so 9 is actually october, subtract 1 to compensate
        // array is 'year', 'month', 'day', etc
        var startDate = moment([year, month - 1]);
    
        // Clone the value before .endOf()
        var endDate = moment(startDate).endOf('month');
    
        // just for demonstration:
        console.log(startDate.toDate());
        console.log(endDate.toDate());
    
        // make sure to call toDate() for plain JavaScript date type
        return { start: startDate, end: endDate };
    }
    

    References:

    • endOf()
    • clone()
    • Date from object
    0 讨论(0)
  • 2020-11-28 09:02

    const year = 2014;
    const month = 09;
    
    // months start at index 0 in momentjs, so we subtract 1
    const startDate = moment([year, month - 1, 01]).format("YYYY-MM-DD");
    
    // get the number of days for this month
    const daysInMonth = moment(startDate).daysInMonth();
    
    // we are adding the days in this month to the start date (minus the first day)
    const endDate = moment(startDate).add(daysInMonth - 1, 'days').format("YYYY-MM-DD");
    
    console.log(`start date: ${startDate}`);
    console.log(`end date:   ${endDate}`);
    <script
    src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js">
    </script>

    0 讨论(0)
  • 2020-11-28 09:02
    const dates = getDatesFromDateRange("2014-05-02", "2018-05-12", "YYYY/MM/DD", 1);           
    console.log(dates);
    // you get the whole from-to date ranges as per parameters
    var onlyStartDates = dates.map(dateObj => dateObj["to"]);
    console.log(onlyStartDates);
    // moreover, if you want only from dates then you can grab by "map" function
    
    function getDatesFromDateRange( startDate, endDate, format, counter ) {
        startDate = moment(startDate, format);
        endDate = moment(endDate, format);
    
        let dates = [];
        let fromDate = startDate.clone();
        let toDate = fromDate.clone().add(counter, "month").startOf("month").add(-1, "day");
        do {
            dates.push({
                "from": fromDate.format(format),
                "to": ( toDate < endDate ) ? toDate.format(format) : endDate.format(format)
            });
            fromDate = moment(toDate, format).add(1, "day").clone();
            toDate = fromDate.clone().add(counter, "month").startOf("month").add(-1, "day");
        } while ( fromDate < endDate );
        return dates;
    }
    

    Please note, .clone() is essential in momentjs else it'll override the value. It seems in your case.

    It's more generic, to get bunch of dates that fall between dates.

    0 讨论(0)
  • 2020-11-28 09:03
    var d = new moment();
    var startMonth = d.clone().startOf('month');
    var endMonth = d.clone().endOf('month');
    console.log(startMonth, endMonth);
    

    doc

    0 讨论(0)
  • 2020-11-28 09:04

    Try the following code:

    const moment=require('moment');
    console.log("startDate=>",moment().startOf('month').format("YYYY-DD-MM"));
    console.log("endDate=>",moment().endOf('month').format("YYYY-DD-MM"));
    
    0 讨论(0)
提交回复
热议问题