我正在运行一个世界时钟脚本(纽约,伦敦,东京,悉尼)时间。在页面上,如果打开/关闭,则在位置名称旁边显示一个基于市场状况的红色/绿色点。
因此,我试图为每个特定的位置设置一个不同的条件,例如:
...between这一次状态显示颜色(石灰绿色),如果关闭这一次它显示状态颜色(红色).
希望我能明白,我渴望很久才能解决这个问题。

function worldClock(zone, region) {
var dst = 0
var time = new Date()
var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000)
var gmtTime = new Date(gmtMS)
var day = gmtTime.getDate()
var month = gmtTime.getMonth()
var year = gmtTime.getYear()
if (year < 1000) {
year += 1900
}
var monthArray = new Array("Jan", "Fev", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec")
var monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
if (year % 4 == 0) {
monthDays = new Array("31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}
if (year % 100 == 0 && year % 400 != 0) {
monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}
var hr = gmtTime.getHours() + zone
var min = gmtTime.getMinutes()
var sec = gmtTime.getSeconds()
var status = new Array("")
if (hr >= 24) {
hr = hr - 24
day -= -1
}
if (hr < 0) {
hr -= -24
day -= 1
}
if (hr < 10) {
hr = " " + hr
}
if (min < 10) {
min = "0" + min
}
if (sec < 10) {
sec = "0" + sec
}
if (day <= 0) {
if (month == 0) {
month = 11
year -= 1
} else {
month = month - 1
}
day = monthDays[month]
}
if (day > monthDays[month]) {
day = 1
if (month == 11) {
month = 0
year -= -1
} else {
month -= -1
}
}
if (region == "NAmerica") {
var startDST = new Date()
var endDST = new Date()
startDST.setMonth(3)
startDST.setHours(2)
startDST.setDate(1)
var dayDST = startDST.getDay()
if (dayDST != 0) {
startDST.setDate(8 - dayDST)
} else {
startDST.setDate(1)
}
endDST.setMonth(9)
endDST.setHours(1)
endDST.setDate(31)
dayDST = endDST.getDay()
endDST.setDate(31 - dayDST)
var currentTime = new Date()
currentTime.setMonth(month)
currentTime.setYear(year)
currentTime.setDate(day)
currentTime.setHours(hr)
if (currentTime >= startDST && currentTime < endDST) {
dst = 1
}
}
if (region == "Europe") {
var startDST = new Date()
var endDST = new Date()
startDST.setMonth(2)
startDST.setHours(1)
startDST.setDate(31)
var dayDST = startDST.getDay()
startDST.setDate(31 - dayDST)
endDST.setMonth(9)
endDST.setHours(0)
endDST.setDate(31)
dayDST = endDST.getDay()
endDST.setDate(31 - dayDST)
var currentTime = new Date()
currentTime.setMonth(month)
currentTime.setYear(year)
currentTime.setDate(day)
currentTime.setHours(hr)
if (currentTime >= startDST && currentTime < endDST) {
dst = 1
}
}
if (hr >= 9 && hr < 17) {
status = "limegreen";
}
if (time.getDay() == 6 || time.getDay() == 0) {
status = "red";
} else {
status = "red";
}
if (dst == 1) {
hr -= 55 - 1
if (hr >= 24) {
hr = hr - 24
day -= -1
}
if (hr < 10) {
hr = " " + hr
}
if (day > monthDays[month]) {
day = 1
if (month == 11) {
month = 0
year -= -1
} else {
month -= -1
}
}
return '<i class="fa fa-circle" style="color: ' + status + ';"></i><br>' + monthArray[month] + " " + day + " " + "<br>" + hr + ":" + min + ":" + sec + " " + "<br>"
} else {
return '<i class="fa fa-circle" style="color: ' + status + ';"></i><br>' + monthArray[month] + " " + day + " " + "<br>" + hr + ":" + min + ":" + sec + " " + "<br>"
}
}
function worldClockZone() {
document.getElementById("Newyork").innerHTML = worldClock(-5, "NAmerica")
document.getElementById("London").innerHTML = worldClock(0, "Europe")
document.getElementById("Tokyo").innerHTML = worldClock(-15, "Greenwich")
document.getElementById("Sydney").innerHTML = worldClock(-14, "Greenwich")
setTimeout("worldClockZone()", 1000)
}
window.onload = worldClockZone;<table style="margin-top: -12px; margin-right: 5px;">
<tr class="hrow">
<td><h5 style="font-size: 11px;">NY</h5> <span id="Newyork" class="user-status" style="font-size: 11px;"></span></td>
<td><h5 style="font-size: 10px;">London </h5> </i><span id="London" class="user-status" style="font-size: 11px;"></span><div style="border-left:1px solid #D3D3D3;height:40px;position: fixed; margin-top: -40px;"></div></td>
<td><h5 style="font-size: 11px;">Tokyo</h5> </i><span id="Tokyo" class="user-status" style="font-size: 11px;"></span><div style="border-left:1px solid #D3D3D3;height:40px;position: fixed; margin-top: -40px;"></div></td>
<td><h5 style="font-size: 11px;">Sydney</h5> <span id="Sydney" class="user-status" style="font-size: 11px;"></span><div style="border-left:1px solid #D3D3D3;height:40px;position: fixed; margin-top: -40px;"></div></td>
</tr>
</table>
发布于 2020-11-13 19:15:08
麻烦在于你对人力资源的评估,这导致了你的地位。正如所写,状态将永远是红色的。这里有一个解决办法:
function worldClock(zone, region){
var dst = 0
var time = new Date()
var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000)
var gmtTime = new Date(gmtMS)
var day = gmtTime.getDate()
var month = gmtTime.getMonth()
var year = gmtTime.getYear()
if(year < 1000){
year += 1900
}
var monthArray = new Array("Jan", "Fev", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec")
var monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
if (year%4 == 0){
monthDays = new Array("31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}
if(year%100 == 0 && year%400 != 0){
monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}
var hr = gmtTime.getHours() + zone
var min = gmtTime.getMinutes()
var sec = gmtTime.getSeconds()
var status = new Array("")
if (hr >= 24){
hr = hr-24
day -= -1
}
if (hr < 0){
hr -= -24
day -= 1
}
if (hr < 10){
hr = " " + hr
}
if (min < 10){
min = "0" + min
}
if (sec < 10){
sec = "0" + sec
}
if (day <= 0){
if (month == 0){
month = 11
year -= 1
}
else{
month = month -1
}
day = monthDays[month]
}
if(day > monthDays[month]){
day = 1
if(month == 11){
month = 0
year -= -1
}
else{
month -= -1
}
}
if (region == "NAmerica"){
var startDST = new Date()
var endDST = new Date()
startDST.setMonth(3)
startDST.setHours(2)
startDST.setDate(1)
var dayDST = startDST.getDay()
if (dayDST != 0){
startDST.setDate(8-dayDST)
}
else{
startDST.setDate(1)
}
endDST.setMonth(9)
endDST.setHours(1)
endDST.setDate(31)
dayDST = endDST.getDay()
endDST.setDate(31-dayDST)
var currentTime = new Date()
currentTime.setMonth(month)
currentTime.setYear(year)
currentTime.setDate(day)
currentTime.setHours(hr)
if(currentTime >= startDST && currentTime < endDST){
dst = 1
}
}
if (region == "Europe"){
var startDST = new Date()
var endDST = new Date()
startDST.setMonth(2)
startDST.setHours(1)
startDST.setDate(31)
var dayDST = startDST.getDay()
startDST.setDate(31-dayDST)
endDST.setMonth(9)
endDST.setHours(0)
endDST.setDate(31)
dayDST = endDST.getDay()
endDST.setDate(31-dayDST)
var currentTime = new Date()
currentTime.setMonth(month)
currentTime.setYear(year)
currentTime.setDate(day)
currentTime.setHours(hr)
if(currentTime >= startDST && currentTime < endDST){
dst = 1
}
}
if(time.getDay() == 6 || time.getDay() == 0){
status = "red";
}
else if (hr >= 9 && hr < 17) {
status = "limegreen";
}
else {
status = "red";
}
if (dst == 1){
hr -=55 -1
if (hr >= 24){
hr = hr-24
day -= -1
}
if (hr < 10){
hr = " " + hr
}
if(day > monthDays[month]){
day = 1
if(month == 11){
month = 0
year -= -1
}
else{
month -= -1
}
}
return '<i class="fa fa-circle" style="color: ' + status + ';"></i><br>' + monthArray[month] + " " + day + " " + "<br>" + hr + ":" + min + ":" + sec + " " + "<br>"
}
else{
return '<i class="fa fa-circle" style="color: ' + status + ';"></i><br>' + monthArray[month] + " " + day + " " + "<br>" + hr + ":" + min + ":" + sec + " " + "<br>"
}
}
function worldClockZone(){
document.getElementById("Newyork").innerHTML = worldClock(-5, "NAmerica")
document.getElementById("London").innerHTML = worldClock(0, "Europe")
document.getElementById("Tokyo").innerHTML = worldClock(-15, "Greenwich")
document.getElementById("Sydney").innerHTML = worldClock(-14, "Greenwich")
setTimeout("worldClockZone()", 1000)
}
worldClockZone()<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/js/all.min.js"></script>
<div id="Newyork"></div>
<div id="London"></div>
<div id="Tokyo"></div>
<div id="Sydney"></div>
发布于 2020-11-13 20:44:05
对于你的问题有很多答案,买你的代码看起来比它需要的要复杂得多。考虑利用Intl.DateTimeFormat构造函数和formatToParts。以下只是使用时间,你可能想包括一周中的一天,假期等。
function isMarketOpen(loc, tz, open, close, date = new Date()) {
// Get time in particular location for given date
let f = new Intl.DateTimeFormat('default', {
hour: '2-digit',
minute: '2-digit',
timeZone: tz,
timeZoneName: 'short'
});
let {hour, minute, timeZoneName} = f.formatToParts(date).reduce((acc, part) => {
if (part.type != "literal") {
acc[part.type] = part.value;
}
return acc;
}, Object.create(null));
// Return based on whether it's between open and close or not
let time = hour*60 + minute*1;
return `${loc} is ${time >= open && time <= close? 'open':'closed'} (${hour}:${minute} ${timeZoneName}).`;
}
// Sample data
let data = [
{loc: 'New York',
tz: 'America/New_York',
open: 540, // Minutes
close: 1020
},{
loc: 'London',
tz: 'Europe/London',
open: 480,
close: 960
}
];
// Show open/closed
data.forEach(obj => console.log(
isMarketOpen(obj.loc, obj.tz, obj.open, obj.close)
));
https://stackoverflow.com/questions/64826289
复制相似问题