使用javascript或jquery获取当月的第一个和最后一个日期

问题描述:


可能重复:

在JavaScript中计算最后一个月的日期

使用javascript确定一个月的天数的最佳方法是什么?

正如标题所示,我坚持找到一种方法来获得第一个和最后一个日期当前月份使用javascript或jquery,并将其格式化为:

As title says, i'm stuck on finding a way to get the first and last date of the current month with javascript or jquery, and format it as:

例如,十一月份应该是:

For example, for november it should be :

var firstdate = '11/01/2012';
var lastdate = '11/30/2012';


很简单,不需要库:

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

或者您可能更喜欢:

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);



EDIT



有些浏览器会对待两个数字年份为二十世纪,因此:

EDIT

Some browsers will treat two digit years as being in the 20th century, so that:

new Date(14, 0, 1);

给出了1914年1月1日。为避免这一点,创建一个日期,然后使用 setFullYear

gives 1 January, 1914. To avoid that, create a Date then set its values using setFullYear:

var date = new Date();
date.setFullYear(14, 0, 1); // 1 January, 14