如何在Javascript中检查月/年是否大于当前月/年

如何在Javascript中检查月/年是否大于当前月/年,javascript,Javascript,我想检查2014年8月是否大于2014年2月 我如何在Javascript中做到这一点??谁能帮帮我吗 目前我只检查未来一年。下面是代码片段 this.isDateGreaterThanCurrent=function(b){ return parseInt(b)>parseInt(new Date().getFullYear()); }; 你就快到了。。。 使用split方法创建过期的数组 this.isDateGreaterThanCurrent=function(b){

我想检查2014年8月是否大于2014年2月

我如何在Javascript中做到这一点??谁能帮帮我吗

目前我只检查未来一年。下面是代码片段

this.isDateGreaterThanCurrent=function(b){
    return parseInt(b)>parseInt(new Date().getFullYear());
};
你就快到了。。。 使用split方法创建过期的数组

this.isDateGreaterThanCurrent=function(b){

var a = b.split('/');

if(parseInt(a[1])>parseInt(new Date().getFullYear())) return true;
else if (parseInt(a[1]) == parseInt(new Date().getFullYear()) && parseInt(a[0])>parseInt(new Date().getMonth())+1)return true;

else return false;
};
别忘了getMonth返回的值是从0到11,而不是从1到12

然后

然而,这可以用手来完成,不需要太多的努力

创建一个接受“MM/YYYY”并返回“YYYY-MM”的函数。然后可以将结果与标准字符串比较进行比较。主要的第一个组件布局是这样的格式很好的一个原因——即使作为字符串,它也可以很容易地排序

function toPartialIso(str) {
    var m = str.match(/(\d{2})\/(\d{4})/);
    if (m) {
        return m[2] + "-" + m[1]
    }
    throw "bad date!"
}

toPartialIso("08/2014") < toPartialIso("02/2014")  // false
toPartialIso("08/2014") > toPartialIso("02/2014")  // true
函数toPartialIso(str){
var m=str.match(/(\d{2})\/(\d{4})/);
如果(m){
返回m[2]+“-”+m[1]
}
抛出“糟糕的约会!”
}
toPartialIso(2014年8月)toPartialIso(2014年2月)//正确

使用

new Date(year, month)  //month start from 0 (ie january is 0)
作为

现在检查一下

Date.parse(d1) > Date.parse(d2)

Date.parse()
,解析日期的字符串表示形式,并返回自1970年1月1日当地时间00:00:00以来的毫秒数。

要比较您格式的两个日期,我们必须解析日期,提取月份和年份并进行比较

建议使用一些第三方库

  • 如果要手动执行此操作,示例代码如下所示

    function checkMonths(month1, month2) {
            var m1 = month1.split("/");
            var m2 = month2.split("/");
            var res = -1;
    
            if( parseInt(m1[1] ) > parseInt(m2[1]) ) {
                res = 1;
            } else if(parseInt(m1[1] ) == parseInt(m2[1])) {
                if(parseInt(m1[0] ) > parseInt(m2[0])) {
                    res = 1;
                } else if(parseInt(m1[0] ) == parseInt(m2[0])) {
                    res = 0;
                } 
            }
            return res;
    }
    

    工作演示

    日期
    对象可以相互比较。因此,您可以将其作为
    date
    对象本身进行解析,而不是解析它们

    function isDateGreaterThanToday(b) {
        var dS = b.split("/");
        var d1 = new Date(dS[1], (+dS[0] - 1));
        var today = new Date();
        if (d1 > today) {
            return "D is greater";
        } else {
            return "today is greater";
        }
    }
    
    console.log(isDateGreaterThanToday("08/2014"))
    

    那么我更喜欢<代码>时刻(“2014年8月”、“2014年9月”).isBefore(时刻(“2014年2月”、“2014年9月”)。然而,你完全可以用手来做这件事。我将首先创建一个函数,该函数接受“02/2014”并返回“2014-02”;然后可以在词汇上与标准字符串比较进行比较。
    function checkMonths(month1, month2) {
            var m1 = month1.split("/");
            var m2 = month2.split("/");
            var res = -1;
    
            if( parseInt(m1[1] ) > parseInt(m2[1]) ) {
                res = 1;
            } else if(parseInt(m1[1] ) == parseInt(m2[1])) {
                if(parseInt(m1[0] ) > parseInt(m2[0])) {
                    res = 1;
                } else if(parseInt(m1[0] ) == parseInt(m2[0])) {
                    res = 0;
                } 
            }
            return res;
    }
    
    function isDateGreaterThanToday(b) {
        var dS = b.split("/");
        var d1 = new Date(dS[1], (+dS[0] - 1));
        var today = new Date();
        if (d1 > today) {
            return "D is greater";
        } else {
            return "today is greater";
        }
    }
    
    console.log(isDateGreaterThanToday("08/2014"))