【发布时间】:2020-11-13 18:37:45
【问题描述】:
我正在运行世界时钟脚本(纽约、伦敦、东京、悉尼)时间。并且在页面上,位置名称旁边会根据市场状态显示一个红/绿点(如果开/关)。
所以,我正在尝试为每个特定位置设置不同的条件,例如:
- 纽约市场上午 9 点开市,晚上 17 点收盘
- 伦敦早上 8 点开门,晚上 16 点关门
...在此期间状态显示颜色(柠檬绿),如果关闭此时间则显示状态颜色(红色)...
希望我能清楚地理解,我很想解决这个问题。
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>
【问题讨论】:
-
请edit您的问题添加运行代码的HTML。我添加了一个堆栈片段,因此您可以单击“编辑上面的 sn-p”链接并将 HTML 粘贴到相应的部分。
-
嗨@HereticMonkey,对不起,这是我的第一个问题。我刚才确实用相应的 html 代码编辑了我的问题,但我不确定它是否正确。感谢您的评论兄弟
标签: javascript html date time