How to get last day of the month

后端 未结 9 992
闹比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:18
    var d = new Date();
    m = d.getMonth(); //current month
    y = d.getFullYear(); //current year
    alert(new Date(y,m,1)); //this is first day of current month
    alert(new Date(y,m+1,0)); //this is last day of current month   
    
    0 讨论(0)
  • 2021-02-01 21:20

    This will give you last day of current month.

    var t= new Date();
    alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2021-02-01 21:22

    Do not forget month started with 0 so +1 in month too.

    let enddayofmonth = new Date(year, month, 0).getDate();
    
    0 讨论(0)
  • 2021-02-01 21:28

    function LastDayOfMonth(Year, Month) {
      return new Date((new Date(Year, Month, 1)) - 1);
    }
    
    console.log(LastDayOfMonth(2009, 11))

    Example:

    > LastDayOfMonth(2009, 11)
    Mon Nov 30 2009 23:59:59 GMT+0100 (CET)
    
    0 讨论(0)
  • 2021-02-01 21:32
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
    
    Date lastDayOfMonth = cal.getTime();
    
    0 讨论(0)
提交回复
热议问题