How to get last day of the month

后端 未结 9 994
闹比i
闹比i 2021-02-01 20:37

How can I obtain the last day of the month with the timestamp being 11:59:59 PM?

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 21:21

    Sometimes all you have is a text version of the current month, ie: April 2017.

    //first and last of the current month
    var current_month = "April 2017";
    var arrMonth = current_month.split(" ");
    var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
    
    //even though I already have the values, I'm using date functions to get year and month
    //because month is zero-based
    var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
    
    //use moment,js to format       
    var start = moment(first_day).format("YYYY-MM-DD");
    var end = moment(last_day).format("YYYY-MM-DD");
    

提交回复
热议问题