简体   繁体   English

如何使用jQuery检查月份是否等于12月

[英]How to check that the month is equal to December using jQuery

So I'm very new to jQuery/Javascript and was just trying to make snow appear during the month of December to have a play with dates so I can get used to them for other things. 所以我对jQuery / Javascript很新,并且只是试图在12月份出现雪来玩日期,所以我可以习惯他们用于其他事情。

I can't work out why this doesn't work. 我无法弄清楚为什么这不起作用。 Is it the mix of jQuery/Javascript? 它是jQuery / Javascript的混合?

$(document).ready(function(){
    var d = new Date();
    n = d.getMonth();

    if (n == 11) {
       $.fn.snow();
    }
});

This however does work but doesn't have the date function. 但这确实有效,但没有日期功能。

$(document).ready(function(){
    $.fn.snow();
});

Javascript date.getMonth() is numbered from 0 to 11 and not 1 to 12 so in order to make your code work in november you have to check 10 and not 11 (or add 1 to your variable n ). Javascript date.getMonth()的编号从011而不是112所以为了让你的代码在11月工作,你必须检查10而不是11 (或者在你的变量n加1)。

By the way, you probably missed the var keyword before your n variable. 顺便说一句,您可能在n变量之前错过了var关键字。

try something like this 尝试这样的事情

            var d = new Date();
            n = d.getMonth() + 1;

The getMonth() method returns the month (from 0 to 11) for the specified date, getMonth()方法返回指定日期的月份(从0到11),

Your code will work only in December only if you want to check then you have to pass any date of December month like, 您的代码仅在December only如果您想检查,那么您必须通过December month任何date ,例如,

var d = new Date("12 Dec 2013");
n = d.getMonth(); // will alert 11 now

Full Code 完整代码

$(document).ready(function(){
    var d = new Date("12 Dec 2013");
    n = d.getMonth();

    if (n == 11) {
       $.fn.snow();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM