如何在javascript中检查给定日期是介于fromDate和toDate之间

时间:2013-07-08 07:49:28

标签: javascript sqlite date cordova

我试图在syntex下面使用它,但它不起作用。请帮忙

var acccID = "acc";
    var date = (new Date().getMonth()+1)+"/"+new Date().getDate()+"/"+new Date().getFullYear();
    var dt1 = (new Date().getMonth()+1)+"/1/"+new Date().getFullYear();
    var dt2 = (new Date().getMonth()+1)+"/31/"+new Date().getFullYear();
    var cr = 5000;

$.mobile.eazydb.transaction(function(tx){
            tx.executeSql('INSERT INTO Ledger (Ledger_Account_ID, Ledger_Date, Ledger_Credit, Ledger_Memo)\
                    VALUES("'+acccID+'", "'+date+'", "'+cr+'", "Opening Balanace : Manish")', [],
                function(tx) { alert('ledger entry successfull.');  }, function(err) {   alert('error in inserted : '+err);  
            });
        });

$.mobile.eazydb.transaction(function(tx){
            tx.executeSql('SELECT * FROM Ledger WHERE Ledger_Date BETWEEN "'+dt1+'" AND "'+dt2+'" ORDER BY Ledger_Date DESC, Ledger_Credit', [],
                function(tx, rs){
                    if(rs.rows.length == 0) {
                        alert('No entries found');
                    } else {
                        for(var i = 0; i<rs.rows.length; i++) {
                            var row = rs.rows.item(i);
                            alert('A/C id : '+row['Ledger_Account_ID']+'\n'+
                                  'Ledger Date : '+row['Ledger_Date']+'\n'+
                                  'Ledger Credit : '+row['Ledger_Credit']+'\n'+
                                  'Ledger Memo : '+row['Ledger_Memo']);
                        }
                    }
            });
        });

它只会提醒No entries found

3 个答案:

答案 0 :(得分:0)

您可以使用普通运算符:

if(currentDate > minDate  && currentDate < maxDate){
// Do stuff
}

答案 1 :(得分:0)

这不是解决问题的方法,而是值得注意的事情。您的日期字符串是静态定义的,使它们与任何其他语言环境不兼容。在您的查询中使用ISO日期格式,因为数据库很可能会支持它们并允许本地化:

var acccID = "acc",
    now = new Date(),
    dstrNow = now.toISOString(),
    cr = 5000,
    dtStart, dtEnd,
    dstrStart, dstrEnd;

dtStart = new Date();
dtStart.setDate(1);
dstrStart = dtStart.toISOString();

dtEnd = new Date();
dtEnd.setMonth(dtEnd.getMonth() + 1);
dtEnd.setDate(-1);
dstrEnd = dtEnd.toISOString();

使用dstrStart和dstrEnd相应地替换你的dt1和dt2值。

答案 2 :(得分:0)

此查询SELECT * FROM Ledger WHERE Ledger_Date BETWEEN "'+dt1+'" AND "'+dt2+'" ORDER BY Ledger_Date DESC, Ledger_Credit中存在错误。 如果要在SQL中选择日期范围,则日期必须为yyyy-mm-dd或yyyy / mm / dd格式。所以检查dt1和dt2变量是否采用上述格式,打印上面的select查询并检查日期范围参数。例如,您的查询可能看起来像

SELECT * FROM Ledger WHERE Ledger_Date BETWEEN "2013-10-01" AND "2014-11-01" ORDER BY Ledger_Date DESC