按"日期"过滤javascript对象列表格式为YYYY / MM / DD

时间:2017-08-30 19:04:32

标签: javascript object

尝试过滤js对象列表,只显示过期日期大于或等于今天的记录,其中今天的日期格式为YYYY.MM.DD



 window.members = [
      {
        LastName: "MacKenzie",
        FirstName: "Randy",
        Expires: "2019.08.12"
      },
      {
        LastName: "Ahmed",
        FirstName: "Jamshed",
        Expires: "2017.10.05"
      },
      {
        LastName: "Attaya",
        FirstName: "Jim",
        Expires: "2018.01.12"
      },
    ];
    
$(document).ready(function () {
  window.members.filter(function (members) {
    return members.Expires >= "2017.08.30";
  });


  var html = "";
  for (var i = 0; i < window.members.length; i++) {
    html+="<div class='members-name'>";
    html+=""+window.members[i].LastName+",  ";
    html+=""+window.members[i].FirstName+" </div><div class='members-exp'>";
    html+=""+window.members[i].Expires+"  </div>";
  }

  $("div").html(html);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div"></div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

I've added an item to your array with a date that is before today. As you can see in the output, it is filtered out of the array.

library(broom)
library(magrittr)
library(dplyr)

W <- rep(letters[seq( from = 1, to = 2)], 25)
X <- rnorm(n=50, mean = 10, sd = 5)
Y <- rnorm(n=50, mean = 15, sd = 6)
Z <- rnorm(n=50, mean = 20, sd = 5)
test_data <- data.frame(W, X, Y, Z)

lapply(test_data[c("X", "Y", "Z")],
       function(x, y) t.test(x ~ y),
       y = test_data[["W"]]) %>% 
  lapply(tidy) %>% 
  do.call("rbind", .) %>% 
  mutate(variable = rownames(.))
dplyr

答案 1 :(得分:0)

new Date()会在您的时区返回今天的日期。

var newDate = new Date();
newDate.setHours(0,0,0,0);
for (var i = 0; i < window.members.length; i++) {
   var expiresDate = new Date(window.members[i].Expires);
   //equality operators require you to use getTime()
   if(expiresDate.getTime() >= newDate.getTime()){
       html+="<div class='members-name'>";
       html+=""+window.members[i].LastName+",  ";
       html+=""+window.members[i].FirstName+" </div><div class='members-exp'>";
       html+=""+window.members[i].Expires+"  </div>";
   }

}