我正在尝试在网站上显示营业时间,但我无法将其转换为24小时制 - 我会改变什么才能让它发生?
Cut off code ^
var checkTime = function() {
var today = weekday[now.getDay()];
var timeDiv = document.getElementById('timeDiv');
var dayOfWeek = now.getDay();
var hour = now.getHours();
var minutes = now.getMinutes();
//add AM or PM
var suffix = hour >= 12 ? "PM" : "AM";
// add 0 to one digit minutes
if (minutes < 10) {
minutes = "0" + minutes
};
if ((dayOfWeek == 0 || dayOfWeek == 6) && hour >= 13 && hour <= 23) {
hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
timeDiv.innerHTML = 'it\'s ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re open!';
timeDiv.className = 'open';
} else if ((dayOfWeek == 3 || dayOfWeek == 4 || dayOfWeek == 5) && hour >= 16 && hour <= 23) {
hour = ((hour + 11) % 12 + 1);
timeDiv.innerHTML = 'it\'s ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re open!';
timeDiv.className = 'open';
} else {
if (hour == 0 || hour > 12) {
hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
}
timeDiv.innerHTML = 'It\'s ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re closed!';
timeDiv.className = 'closed';
}
};
var currentDay = weekday[now.getDay()];
var currentDayID = "#" + currentDay; //gets todays weekday and turns it into id
$(currentDayID).toggleClass("today"); //hightlights today in the view hours modal popup
setInterval(checkTime, 1000);
checkTime();
提前致谢。
答案 0 :(得分:1)
试试这个......
// Pad leading zero if number is < 10
function padZero(i){
return i < 10
? "0" + i
: i;
}
// Construct time string
function formatTime(day, hours, minutes, suffix, state){
return "It's " + today + " " + padZero(hours) + ":" + padZero(minutes) + suffix + " - we're " + state + "!";
}
// Output time to screen
function checkTime() {
var today = weekday[now.getDay()];
var timeDiv = document.getElementById('timeDiv');
var dayOfWeek = now.getDay();
var hour = now.getHours();
var minutes = now.getMinutes();
//add AM or PM
//var suffix = hour >= 12 ? "PM" : "AM";
var suffix = "";
if ((dayOfWeek == 0 || dayOfWeek == 6) && hour >= 13 && hour <= 23) {
//hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
timeDiv.innerHTML = formatTime(today, hour, minutes, suffix, "open");
timeDiv.className = 'open';
} else if ((dayOfWeek == 3 || dayOfWeek == 4 || dayOfWeek == 5) && hour >= 16 && hour <= 23) {
//hour = ((hour + 11) % 12 + 1);
timeDiv.innerHTML = formatTime(today, hour, minutes, suffix, "open");
timeDiv.className = 'open';
} else {
/*if (hour == 0 || hour > 12) {
hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
}*/
timeDiv.innerHTML = formatTime(today, hour, minutes, suffix, "closed");
timeDiv.className = 'closed';
}
};
的变化:
suffix
设置为“”(如果需要,可以将其删除)formatTime()
padZero()
函数,以确保10以下的小时和分钟填充前导'0'。