Javascript 查询一个日期是否介于其他两个日期之间?

Javascript 查询一个日期是否介于其他两个日期之间?,javascript,jquery,Javascript,Jquery,使用下面的代码,我试图运行一个查询,其中日期必须在由本地存储变量“name”确定的一年内。因为我只需要与“value”中保存的结果在同一年的结果,所以我将年份分隔,将其转换为字符串,现在该年份在变量“string”中是有帮助的 alert(value); var cowid = localStorage.getItem('search_cowid'); //LSV contains the string that will not be modified //search based o

使用下面的代码,我试图运行一个查询,其中日期必须在由本地存储变量“name”确定的一年内。因为我只需要与“value”中保存的结果在同一年的结果,所以我将年份分隔,将其转换为字符串,现在该年份在变量“string”中是有帮助的

   alert(value);

var cowid = localStorage.getItem('search_cowid');
//LSV contains the string that will not be modified
//search based on year--it is the one that is saved when the cow name is clicked on cow_history_search
//var dateyear = localStorage.getItem('name');
//LSV contains the year that will determine if is a match or not.
//I mapped the data to the 'where parameter 
///console.log(name);
///console.log("value = " + value);



var prestring = value;

//var string = prestring.toString("");

alert(prestring);

var beginning = "01"+ "/" + "01" + "/"+ prestring;
alert(beginning);

var end = "12"+ "/" + "31" + "/"+ prestring;
alert(end);


    var whereObject = 
        {"$and": [
            {"date": {"$gte": beginning} },
            {"date": {"$lte": end}} ,
            {"cow_id": cowid}
] };

    return JSON.stringify(whereObject);
示例:“值”是搜索输入框中的值。因此,在这种情况下,这将是一个类似于2016年10月3日的日期。然后将其拆分,使YYYYMMDD的实际值=2016。从那里,它把它放在一个日期的末尾,这个日期将等于一年的第一天和一年的最后一天


问题在于,我认为它无法正确检测日期是否在开始和结束之间,因为它返回的值与cowed匹配,但没有考虑日期因素。

此“查询”活动在哪里?这段代码所做的只是构建一个对象并将其字符串化。@Pointy在没有返回stringify的情况下对其进行测试后,我得到了与以前相同的结果。您实际在哪里比较开始和结束日期?在任何情况下,由于您从变量中选取了年份,并创建了具有相同年份值的年初日期和年末日期,因此变量日期必须介于两个限制之间。@sideroxylon确定我要匹配的年份的变量“name”来自本地存储。我已经特别确定了年份值不同于仅用于测试目的的任何结果。您发布的代码中没有任何内容与“查询”相关。代码构建一个数据结构,并将其作为JSON字符串返回;就这样。如果该字符串随后被发送到其他机制,那么您必须对此进行描述。如果您希望该代码神奇地从某处执行某些选择,那么它不会。