简体   繁体   English

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

[英]How to check if a Month/Year is greater than current Month/Year in Javascript

I want to check if 08/2014 is greater than 02/2014.. 我想检查08/2014是否大于02/2014 ..

How can I do that in Javascript?? 我怎么能在Javascript中做到这一点? Could someone help me please... 有人可以帮我吗...

Currently I only check for future year. 目前我只检查未来的一年。 Here is the code snippet. 这是代码片段。

this.isDateGreaterThanCurrent=function(b){
    return parseInt(b)>parseInt(new Date().getFullYear());
};

With moment.js (which I recommend), then .. moment.js (我推荐),然后..

var d1 = moment("08/2014", "MM/YYYY")
var d2 = moment("02/2014", "MM/YYYY")
d1.isBefore(d2) // false
d1.isAfter(d2)  // true

However, this can be done by hand without much effort. 然而,这可以通过手工完成而不需要太多努力。

Create a function that took in "MM/YYYY" and returns "YYYY-MM". 创建一个接收“MM / YYYY”并返回“YYYY-MM”的函数。 The result can then be compared with a standard string compare. 然后可以将结果与标准字符串比较进行比较。 The major-first component layout is one reason why a format like ISO 8601 is nice - even as a string, it can be easily ordered. 主要的第一个组件布局是ISO 8601这样的格式很好的一个原因 - 即使是字符串,也可以轻松订购。

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

construct two date object with 构造两个日期对象

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

as, 如,

d1 = new Date(2014, 7)     // 08/2014   (month - 1)
d2 = new Date(2014, 1)     // 02/2014

Now check, 现在检查,

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

Date.parse() , parses a string representation of a date and returns the number of milliseconds since 1 January, 1970, 00:00:00, local time. Date.parse() ,解析日期的字符串表示形式,并返回自当地时间1970年1月1日00:00:00以来的毫秒数。

Date objects can be compared with each other. Date对象可以相互比较 So instead of parsing them you can do it as date object itself. 因此,您可以将其作为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"))

JSFiddle 的jsfiddle

You're nearly there ... Using the split method to create an array out of your date 你快到了...使用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;
};

dont forget getMonth returns values from 0 to 11, not from 1 to 12 ! 别忘了getMonth返回0到11之间的值,而不是1到12!

To compare two date which are in your format, we have to parse the dates and extract the month and year and compare them 要比较格式中的两个日期,我们必须解析日期并提取月份和年份并进行比较

Its preffered to use some third party library 它优先使用一些第三方库

  1. datejs datejs
  2. momentjs momentjs

If you want to do this manually the sample code looks like 如果您想手动执行此操作,示例代码如下所示

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;
}

Working demo JSFiddle demo 工作演示JSFiddle演示

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

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