我有开放时间在网站上显示。
Mon, Tue, Wed, Fri 10:00 to 18:00
thursday 10:00 to 20:00
saturday: 9:00 to 14:00
现在我需要在主页上显示与当前时间比较的消息
在此时间范围内。显示消息"我们在这里等你"。 当它超出时间范围时,例如:星期一18:15它应该说:
"我们星期二从10:00和#34;再次来到这里。
我该怎么做?你能帮我吗?
代码
<script>
$(function(){
$.get('date.json', function(data){
initialize(data);
});
});
function initialize(data) {
$.each(data,function(pos) {
alert(this.day);
});
}
</script>
JSON
[
{
"day": "monday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "tuesday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "wednesday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "thursday",
"starttime": "10.00",
"endtime": "20.00"
},
{
"day": "friday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "saturday",
"starttime": "9.00",
"endtime": "14.00"
},
{
"day": "sunday",
"starttime": "10.00",
"endtime": "18.00"
}
]
答案 0 :(得分:1)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(function () {
$.get('date.json', function (data) {
initialize(data);
});
});
function checkTime(h, m, a, b, c, d) {
if (a > c || ((a == c) && (b > d))) {
} else {
if (h > a && h < c) {
return true;
} else if (h == a && m >= b) {
return true;
} else if (h == c && m <= d) {
return true;
} else {
return false;
}
}
}
function initialize(data) {
var d = new Date();
var weekday = new Array(7);
weekday[0] = "sunday";
weekday[1] = "monday";
weekday[2] = "tuesday";
weekday[3] = "wednesday";
weekday[4] = "thursday";
weekday[5] = "friday";
weekday[6] = "saturday";
$.each(data, function (pos) {
if (this.day == weekday[d.getDay()]) {
if (checkTime(d.getHours().toString(), d.getMinutes().toString(), this.starttime.split(".")[0], this.starttime.split(".")[1], this.endtime.split(".")[0], this.endtime.split(".")[1]) == true) {
alert("we were here for you");
}
else {
}
}
});
}
</script>
</head>
<body>
</body>
</html>
答案 1 :(得分:1)
将以下内容复制并粘贴到控制台。我使用了与您提供的完全相同的数据。随意提出任何问题或报告任何问题。
var dataJSON = [
{
"day": "monday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "tuesday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "wednesday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "thursday",
"starttime": "10.00",
"endtime": "20.00"
},
{
"day": "friday",
"starttime": "10.00",
"endtime": "18.00"
},
{
"day": "saturday",
"starttime": "9.00",
"endtime": "14.00"
},
{
"day": "sunday",
"starttime": "10.00",
"endtime": "18.00"
}
];
var d = new Date();
var day = d.toString().split(' ')[0].toLowerCase();
var currHR = d.getHours();
for(i = 0; i<7; i++) {
if(((dataJSON[i].day)).indexOf(day) > -1) {
console.log(day);
console.log(dataJSON[i].starttime + " "+dataJSON[i].endtime)
if(currHR >= dataJSON[i].starttime && currHR < dataJSON[i].endtime) {
console.log("We are here");
} else {
if(i==6) {
i = 0;
} else {
i = i+1;
}
console.log("we are here for you again "+ dataJSON[i].day + " from " + dataJSON[i].starttime +" to " +dataJSON[i].endtime);
}
}
}