Javascript 使用 jquery 获取当前月份和日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12129130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:10:45  来源:igfitidea点击:

Get current month and date using jquery

javascriptjqueryrazor

提问by Suraj Shrestha

I have two dropdownlist that hold months and years...

我有两个包含数月和数年的下拉列表...

$(document).ready(function () {               
     $('#Months option:eq(' + (new Date).getMonth() + ')').prop('selected', true);
      alert((new Date).getMonth());        
     $('#Years option:eq(' + (new Date).getFullYear() + ')').prop('selected', true);
      alert((new Date).getFullYear());
});

I wrote the JQuery script as above so that when my program runs the dropdown selected value must be the current month and year..

我写了上面的 JQuery 脚本,这样当我的程序运行时,下拉选择的值必须是当前的月份和年份..

but when i execute the program.. the alert gives 7 and 2012.. but in my view the current month is selected but the current year is not why?? and how can I make my dropdown to select current year??

但是当我执行程序时.. 警报给出了 7 和 2012.. 但在我看来,当前月份被选中,但当前年份不是为什么?以及如何让我的下拉菜单选择当前年份?

回答by snuffn

jQuery

jQuery

var d = new Date(),

    n = d.getMonth(),

    y = d.getFullYear();

$('#months option:eq('+n+')').prop('selected', true);

$('#years option[value="'+y+'"]').prop('selected', true);

HTML

HTML

?<select id="months">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
</select>
<select id="years">
    <option value="2011">2011</option>
    <option value="2012">2012</option>
    <option value="2013">2013</option>
</select>

DEMO

演示

回答by jussinen

Months are returned as an integer from 0 (January) to 11 (December): see http://www.java2s.com/Tutorial/JavaScript/0240__Date/DategetMonth.htm

月份作为从 0(一月)到 11(十二月)的整数返回:请参阅http://www.java2s.com/Tutorial/JavaScript/0240__Date/DategetMonth.htm

So the current month (August) would return 7. To be able to match up with the usual month index (meaning 1 to 12), use ((new Date()).getMonth() + 1).

因此当前月份(八月)将返回 7。为了能够与通常的月份索引(即 1 到 12)匹配,请使用((new Date()).getMonth() + 1).

As pointed out in the comments, I misunderstood the problem. The month is correct because of the zero-based array for months and the jquery selector :eq()being also zero-based.

正如评论中指出的那样,我误解了这个问题。月份是正确的,因为月份的数组从零开始,而 jquery 选择器:eq()也是从零开始的。

However as pointed in other answers, the year returned would not match :eq()which matches the nth element in the jquery selector. Using as suggested option[value=' + (new Date).getFullYear() + ')'would be better.

但是,正如其他答案中所指出的,返回的年份:eq()与 jquery 选择器中的第 n 个元素不匹配。按照建议使用option[value=' + (new Date).getFullYear() + ')'会更好。

回答by lqc

The :eq(N)part of the selector in jQuery selects Nth element (including 0th) in the matched set (which at that point would be #Years option). So unless you have more then 2012 options in the Yearsdropdown, this won't work.

:eq(N)jQuery中选择第N个元素(包括0)级的匹配组(其在该点处将是选择器的一部分#Years option)。因此,除非Years下拉列表中有超过 2012 年的选项,否则这是行不通的。

It's better to select the option based on it's value: option[value=2012]or

最好根据其值选择选项:option[value=2012]

just use $("#Years").val(2012), it's more readable and less code.

只需使用$("#Years").val(2012),它的可读性更高,代码更少。