我有以下时间功能,用于在公司营业或关闭时输出。我的编码方式在正常的工作周内效果很好。我想要做的是将其编程为假期,例如圣诞节,7月4日等总是在同一天的假期,因此输出显示我们已关闭。
在固定的假期我将如何编程?
function timeNow() {
//Get time
var d = new Date(),
h = ('0' + d.getHours()).slice(-2),
m = ('0' + d.getMinutes()).slice(-2);
//Get Day
new Date().getDay();
var day = new Date().getDay();
console.log(day);
var operateDay = '';
if (day >= 1 && day <= 5) {
operateDay = true;
} else if (day < 1 || day > 5) {
operateDay = false;
}
// Checking time range
var operatingTime = ((h >= 8 && h < 17) || (h === 8 && m == 0));
var closedTime = (h >= 17 || h <= 8);
var operateStatus = '';
if ((operatingTime) && (operateDay = true)) {
operateStatus = 'Our office is currently open';
$('#closedWrap').addClass('block');
$('#operationStatus').addClass('open');
} else if (closedTime) {
operateStatus = 'Our office is currently closed';
$('#closedWrap').addClass('block');
$('#operationStatus').addClass('closed');
} else if (operateDay = false) {
operateStatus = 'Our office is currently closed';
$('#closedWrap').addClass('block');
$('#operationStatus').addClass('closed');
}
return operateStatus;
}
var operation = timeNow();
$('#operationStatus').html(operation);
答案 0 :(得分:0)
我已经修改了您的原始功能,并创建了初始版本,该版本处理了自定义的,易于“配置”的,人类可读的特定假期列表。
/**
* Returns the office's open/closed state.
*
* @param {Date} [time] - A custom Date object to override the current time.
* @returns {string} The state of the office (open or closed).
*/
function timeNow(time) {
// Holidays - month-day format, it's adjusted for human-readability, so
// January is 1, February is 2 ... December is 12
var holidays = [
'01-01', // New Year's Day
'07-04', // Independence Day,
'11-11', // Veterans Day
'12-25', // Chirstmas
'12-31', // New Year's Eve
];
// Possibility of overriding the current date, if needed
time = time || new Date();
// Get time
var d = time,
h = ('0' + d.getHours()).slice(-2),
m = ('0' + d.getMinutes()).slice(-2);
// Get day
var day = time.getDay();
var operateDay = '';
if (day >= 1 && day <= 5) {
operateDay = true;
} else if (day < 1 || day > 5) {
operateDay = false;
}
// Checking time range
var operatingTime = ((h >= 8 && h < 17) || (h === 8 && m == 0));
var closedTime = (h >= 17 || h <= 8);
var operateStatus = '';
// Check against holidays if the store is seemingly open
// Since in JavaScipt's built-in Date object, January is 0, February is 1 ... December is 11,
// to be humanly usable, add 1 to it, hence the name "adjusted"
var adjustedMonth = time.getMonth() + 1;
var dayOfMonth = time.getDate();
if (operatingTime) {
for (var i = 0, len = holidays.length, holiday, holidayMonth, holidayDay; i < len; i++) {
// Process the specific holiday month-date format
holiday = holidays[i].split('-');
holidayMonth = parseInt(holiday[0], 10);
holidayDay = parseInt(holiday[1], 10);
// Check, whether today is a holiday ...
if (adjustedMonth === holidayMonth && dayOfMonth === holidayDay) {
// ...and if it is, then ...Hooray! Hooray! It's a Holi-Holiday
operatingTime = false;
closedTime = true;
break;
}
}
}
if ((operatingTime) && (operateDay = true)) {
operateStatus = 'Our office is currently open';
$('#closedWrap').addClass('block');
$('#operationStatus').addClass('open');
} else if (closedTime) {
operateStatus = 'Our office is currently closed';
$('#closedWrap').addClass('block');
$('#operationStatus').addClass('closed');
} else if (operateDay = false) {
operateStatus = 'Our office is currently closed';
$('#closedWrap').addClass('block');
$('#operationStatus').addClass('closed');
}
return operateStatus;
}
// Testing
var shouldBeOpen = [
'2018-06-25',
'2018-06-26',
'2018-06-27',
'2018-06-28',
'2018-06-29'
];
var shouldBeClosed = [
'2018-01-01',
'2018-07-04',
'2018-11-11',
'2018-12-25',
'2018-12-31',
'2019-01-01'
];
var STATUS_OPEN = 'Our office is currently open';
var STATUS_CLOSED = 'Our office is currently closed';
// converts a human-readable year-month-day to UTC date
function strToUtcDate(string) {
var parts = string.split('-');
var year = parts[0];
var month = parts[1];
var day = parts[2];
return new Date(Date.UTC(year, month - 1, day, 12, 0, 0));
}
shouldBeOpen.forEach(function (string) {
var date = strToUtcDate(string);
var status = timeNow(date);
if (status === STATUS_OPEN) {
console.log('opened, as it should on:', date);
} else {
console.log('closed, BUT IT SHOULD NOT ON:', date);
}
});
shouldBeClosed.forEach(function (string) {
var date = strToUtcDate(string);
var status = timeNow(date);
if (status === STATUS_CLOSED) {
console.log('closed, as it should on:', date);
} else {
console.log('opened, BUT IT SHOULD NOT ON:', date);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>