我的目标是显示我的营业时间,并指出我们何时关闭。
查看我要在codepen上执行的操作的示例。但这是24小时格式。我需要转换javascript以12小时格式显示日期的帮助。
下面是显示24小时开放时间的javascript。
jQuery(document).ready(function($){
var currentDate = new Date();
var weekday = [];
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var currentDay = weekday[currentDate.getDay()];
var currentTimeHours = currentDate.getHours();
currentTimeHours = currentTimeHours < 10 ? "0" + currentTimeHours : currentTimeHours;
var currentTimeMinutes = currentDate.getMinutes();
var timeNow = currentTimeHours + "" + currentTimeMinutes;
var currentDayID = "#" + currentDay; //gets todays weekday and turns it into id
$(currentDayID).toggleClass("today"); //this works at hightlighting today
var openTimeSplit = $(currentDayID).children('.opens').text().split(":");
var openTimeHours = openTimeSplit[0];
openTimeHours = openTimeHours < 10 ? "0" + openTimeHours : openTimeHours;
var openTimeMinutes = openTimeSplit[1];
var openTimex = openTimeSplit[0] + openTimeSplit[1];
var closeTimeSplit = $(currentDayID).children('.closes').text().split(":");
var closeTimeHours = closeTimeSplit[0];
closeTimeHours = closeTimeHours < 10 ? "0" + closeTimeHours : closeTimeHours;
var closeTimeMinutes = closeTimeSplit[1];
var closeTimex = closeTimeSplit[0] + closeTimeSplit[1];
if (timeNow >= openTimex && timeNow <= closeTimex) {
$(".openorclosed").toggleClass("open");
} else {
$(".openorclosed").toggleClass("closed");
}
});
您可以在codepen上以24小时格式查看工作示例
提前感谢您的帮助和时间!
答案 0 :(得分:0)
var currentTimeHours = currentDate.getHours();
currentTimeHours = currentTimeHours < 10 ? "0" + currentTimeHours : currentTimeHours;
var currentTimeMinutes = currentDate.getMinutes();
var timeNow = currentTimeHours + "" + currentTimeMinutes;
Replace the above code by
var currentTimeHours = currentDate.getHours();
var currentTimeMinutes = currentDate.getMinutes();
var timeNow = currentTimeHours % 12 + ':' + currentTimeMinutes + ':'+ (currentTimeHours < 12 ? 'AM':'PM');
答案 1 :(得分:0)
var openTimeHours = openTimeSplit[0];
openTimeHours = openTimeHours < 10 ? "0" + openTimeHours : openTimeHours;
var openTimeMinutes = openTimeSplit[1];
var openTimex = openTimeSplit[0] + openTimeSplit[1];
Replace with the bellow codes
var openTimeHours = parseInt(openTimeSplit[0]);
var openTimeMinutes = openTimeSplit[1];
var openTimex = openTimeHours % 12 + ':' + openTimeMinutes + ':' + (openTimeHours < 12 ?'AM':'PM');