使用javascript或jquery获取当月的第一个和最后一个日期 [英] Get first and last date of current month with javascript or jquery

查看:97
本文介绍了使用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

这篇关于使用javascript或jquery获取当月的第一个和最后一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆