如何在javascript中获得给定的月份开始日期和结束日期

How can I get given month start date and end date in javascript

本文关键字:日期 开始 结束 javascript      更新时间:2023-09-26

我需要得到一个月的开始日期和结束日期。

   var currentDate = new Date();
   var currentMonth = currentDate.getMonth();

试试这个:

function MyCtrl($scope){
   var date = new Date(), year = date.getFullYear(), month = date.getMonth();
   var firstDay = new Date(year, month, 1);
   var lastDay = new Date(year, month + 1, 0);
   $scope.firstDay = firstDay ;
   $scope.lastDay = lastDay ;
   $scope.date = date ;    
}

和:

<div ng-app ng-controller="MyCtrl">
<ul>
    <li>{{date | date:'yyyy-MM-dd'}}</li> 
    <li>{{firstDay | date:'yyyy-MM-dd'}}</li> 
    <li>{{lastDay | date:'yyyy-MM-dd'}}</li> 
</ul>