2016-06-29 138 views
-1

我只有一个月e.g:“如何在Javascript中获取月份的开始日期和结束日期?

从这个我想开始日期最后“五一”一个月使用Java脚本的日期

谢谢。

+2

开始日期始终为“1 “(例如”5月1日“) – Buddy

+0

它已经在stackoverflow [这里]回答(http://stackoverflow.com/questions/13571700/get-first-and-last-date-of-current-month-with-javascript-或-jquery) –

+2

[计算javascript中的最后一天的日期](http://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript) – Dave

回答

1

任何一个月的第一天是1,至于最后日期而言,你可以使用moveToLastDayOfMonth

移动的日期到该月的最后一天。

,或者你可以用这样的方式:

var month = 4; 
 
var d = new Date(2016, month + 1, 0); 
 
alert(d);

+0

需要'-1'而不是'0'来获取当前月份的长度。 – dandavis

+0

@dandavis: - 我已经将月份设置为'month + 1'。所以我不认为它是必需的。 –

+0

@dandavis:''我想要使用java-script.'获取'May'月的开始日期和最后日期。所以开始日期总是1,我已经提到,最后日期将是31,以防万一。 –

0

试试这个:

date = new Date(2016, 5, 1); 
 
lastdate = new Date(date - 1); 
 
console.log(lastdate)

0

开始日期将永远是1, 你可以试试这个最后日期,

var date = new Date(); 
var lastDate = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); 
0

谢谢你的回应。

我找到了答案。

var month = 'MAY' 
var a = '1-'+month+'-2015'; 
     var date = new Date(a); 
     var firstDay = new Date(date.getFullYear(), date.getMonth(), 1); 
     var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0); 
     console.log(firstDay); 
     console.log(lastDay); 
0

我创建为JS标准date.Then获取日期格式日期检查月份值(年,月,日)个月阵列,只要你想

var months = ["January","February","March","April","May","June","July","August","September","October","November","December"]; 
var month = "May"; 
for(var i in months){ 
    if(months[i] == month){ 
     month = i; 
    } 
} 
var s = new Date(2016, month, 1); 
var e = new Date(2016, ++month, 0); 
console.log(s+"\n"+e); 
相关问题