我有一个HTML页面,包含3个月,日和年的下拉菜单,我想知道是否有办法根据月份和年份正确填充月份下拉列表。
我之前没有在客户端做过这个,但看起来像jQuery DatePicker这样的很多控件都是在幕后做的。
答案 0 :(得分:27)
据我所知,没有(整洁的)内置功能。我曾写过一次:
// note that month is 0-based, like in the Date object. Adjust if necessary.
function getNumberOfDays(year, month) {
var isLeap = ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
return [31, (isLeap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
答案 1 :(得分:18)
您可以使用日期对象:
var monthStart = new Date(year, month, 1);
var monthEnd = new Date(year, month + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24)
使用Date
个对象的算术给出了毫秒数。
这甚至适用于12月; Date构造函数通过环绕来处理超出范围的参数。
请注意,month
从零开始(必须介于0
和11
之间)
答案 2 :(得分:3)
从其他帖子复制:Get number days in a specified month using javascript?
//Month is 1 based
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
//July
daysInMonth(7,2009); //31
//February
daysInMonth(2,2009); //28
daysInMonth(2,2008); //29
@c_harm的所有学分,真的很棒的解决方案
答案 3 :(得分:2)
Date.prototype.daysinMonth: function(){
var d= new Date(this.getFullYear(), this.getMonth()+1, 0);
return d.getDate();
}
function daysinMonthfromInput(month,year){
return (new Date(year,month-1,1)).daysinMonth();
}
alert(daysinMonthfromInput(2,2011));
答案 4 :(得分:1)
这是一个班轮。 假设您说的是1月= 1,2月= 2等...(正常) 这是闰年的例子:
var y = 2012;
var m = 2;
var daysInMonth = new Date(y,m,1,-1).getDate();
答案 5 :(得分:0)
我在我当前的项目中使用这种方法,发现我需要纠正舍入错误。因此,我不必在代码中使用monthLength,而是使用它:
monthLength.toFixed(0)
例如,如果我有一个存储文本日期字段的对象,它可能如下所示:
obj.month = theMonths[mm - 1] + " " + monthLength.toFixed(0) + ", " + obj.year;
答案 6 :(得分:0)
你实际上可以使用它:
var curdate = new Date(); DaysMonth = 32 - new Date(curdate.getYear(),curdate.getMonth(),32)。getDate();
)
答案 7 :(得分:-2)
class Request(_Request):
"""
HTTP request class.
This is a Pyramid Request object augmented with type hinting information for Websauna-specific functionality.
To know more about request read also
* py:class:`pyramid.request.Request` documentation
* py:class:`webob.request.Request` documentation
Counterintuitively, this request is also available in non-HTTP applications like command line applications and timed tasks.
These applications do not get request URL from a front end HTTP webserver, but a faux request is constructed pointing to the website URL taken from ``websauna.site_url`` setting.
This is to allow similar design patterns and methodology to be applied in HTTP and non-HTTP applications.
By setting variables in ``__type_hinting__()`` based on arguments types allows IDEs to infer type information when you hint your views as::
from websauna.system.http import Request
def hello_world(request: Request):
request. # <-- When typing, here autocompletion kicks in.
"""
def __type_hinting__(self, user: Optional[User], dbsession: Session, session: ISession, admin: Admin, registry: Registry):
"""
A dummy helper function to tell IDEs about reify'ed variables.
:param user: The logged in user. None if the visitor is anonymous.
:param dbsession: Current active SQLAlchemy session
:param session: Session data for anonymous and logged in users.
:param admin: The default admin interface of the site. Note that the site can have several admin interfaces for different purposes.
:param registry: Pyramid registry's. E.g.
:py:attr:`pyramid.registry.Registry.settings` for reading settings and :py:meth:`pyramid.registry.Registry.notify` for sending events.
"""
self.user = user
self.dbsession = dbsession
self.session = session
self.admin = admin
self.registry = registry