te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>How to check if a MonthYear is greater than current MonthYear in Javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if a MonthYear is greater than current MonthYear in Javascript - Stack Overflow

programmeradmin2浏览0评论

I want to check if 08/2014 is greater than 02/2014..

How can I do that in 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());
};

I want to check if 08/2014 is greater than 02/2014..

How can I do that in 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());
};
Share Improve this question asked Feb 6, 2014 at 5:43 AnsumanAnsuman 1,4944 gold badges17 silver badges32 bronze badges 1
  • I prefer moment.js, then .. moment("08/2014", "MM/YYYY").isBefore(moment("02/2014", "MM/YYYY")). However, you're more than free to do this by hand. I would start by creating a function that took in "02/2014" and returns "2014-02"; then that can be pared lexically with a standard string pare. – user2864740 Commented Feb 6, 2014 at 5:50
Add a ment  | 

5 Answers 5

Reset to default 3

Date objects can be pared with each other. So instead of parsing them you can do it as date object itself.

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

With moment.js (which I remend), then ..

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". The result can then be pared with a standard string pare. The major-first ponent layout is one reason why a format like ISO 8601 is nice - even as a string, it can be easily ordered.

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.

You're nearly there ... Using the split method to create an array out of your date

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 !

To pare two date which are in your format, we have to parse the dates and extract the month and year and pare them

Its preffered to use some third party library

  1. datejs
  2. 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

发布评论

评论列表(0)

  1. 暂无评论