将日期字符串与json日期字符串

时间:2017-12-28 01:01:52

标签: javascript jquery html json jquery-ui

用户从两个jquery-ui日期选择器中选择一对日期,当用户选择第二个日期时,将两个值与存储在外部JSON文件中的字符串进行比较。如果日期在下面的日期内,则外部数据将返回到html页面。日期如下:

"startDate":"01/01/2017",
"endDate":"05/09/2018"

我创建一个if语句是不成功的,该语句将成功比较datepicker中的日期字符串,有人可以指出我在制作if语句时出错的地方吗?

JQUERY CODE:

$("#frompicker").datepicker({
    dateFormat: "dd/mm/yy",
    numberOfMonths: 2,
    onSelect: function(selected) {
        $("#topicker").datepicker(null, "minDate", selected);
        date = selected;
        console.log(date);
    }
});
$("#topicker").datepicker({
    dateFormat: "dd/mm/yy",
    numberOfMonths: 2,
    onSelect: comparetime
});
var comparetime = function(selected) {
    $("#frompicker").datepicker(null, "maxDate", selected);
    dateObject = selected;
    $('.removeMe').remove();
    $.getJSON('Holidays.json', function(data) {
        for (i = 0; i < data.Resorts.length; i++) {
            var d = date;
            var x = dateObject;
            if ((d >= data.Resorts[i].startDate && x < data.Resorts[i].endDate)) {
                console.log(date + " " + dateObject);
                var output = "<section><section class='removeMe container'>";
                output += "<h2>" + data.Resorts[i].name + ", " + data.Resorts[i].destination + "<a href='" + data.Resorts[i].url + "'>Take an In-Depth look</a> " + "</h2>" + "<img src='" + data.Resorts[i].picture + ".jpg'>" + "<article>" + "<p>" + data.Resorts[i].short_description + "</p>" + "<p>Rating: " + data.Resorts[i].Rating + "</p>" + "<p>" + "Price: " + data.Resorts[i].price + "</p>" + "<p>Start Date:" + "<time datetime='" + data.Resorts[i].startDate + "'>" + data.Resorts[i].startDate + "</time></p>" + "<p>End Date: " + "<time datetime='" + data.Resorts[i].endDate + "'>" + data.Resorts[i].endDate + "</time></p>" + "<p> Activities: " + data.Resorts[i].activities + "</p>" + "</article></section></section>";
                $("body").append(output + "<br class='removeMe'>" + "<br class='removeMe'>" + "<br class='removeMe'>" + "<br class='removeMe'>" + "<br class='removeMe'>" + "<br class='removeMe'>" + "<br class='removeMe'>" + "<br class='removeMe'>" + "<br class='removeMe'>");
            }
        }

    });
};

JSON格式:

{
      "destination":"Carribean",
      "name":"Les Boucaniers",
      "location":"Martinique",
      "Rating":"4",
      "activities":[
        "WaterSkiing",
        "Tennis",
        "ScubaDiving",
        "Spa"
      ],
      "price":"£1254",
      "startDate":"01/01/2017",
      "endDate":"05/09/2018",
      "short_description":"The resort of Les Boucaniers is located on the laid-back beach-covered south coast of the island, and is perfectly placed for Martinique holidays that are both relaxing and awe-inspiring.",
      "picture":"LesBoucaneers",
      "long_description":"A divers' paradise in the Baie du Marin, a legendary spot.<br>Its bungalows are discreetly lodged in a tropical garden beside the white sand beach in superb Marin Bay. A magical site where you can enjoy a taste of everything, alone or with family or friends. Try water sports and the magnificent Club Med Spa*. You'll be enchanted by the exotic flavours of the local cuisine and the joyful spirit of the Caribbean.",
      "url":"Carribean.html"
}

0 个答案:

没有答案
相关问题