简体   繁体   English

如果不是,则表示javascript中半小时的时间增量

[英]represent half hour time increment in javascript if else if

I have built a simple stock feed that uses xml and javascript. 我建立了一个使用xml和javascript的简单库存提要。 i aded in a conditional based on time so before the market is open it shows the previous close, while its open it shows the ask price, when it is closed, it shows the last price. 我根据时间进行有条件的操作,因此在开市之前,它会显示前一个收盘价;在开市时,它会显示要价;当收市时,它会显示最近的价格。 It works, but only if the time interval is a whole number. 它有效,但仅当时间间隔为整数时。 i need my time intervals to be 9:30 to 4:30 (16:30) but when I try to augment the time to the half hour it errors. 我需要将时间间隔设置为9:30到4:30(16:30),但是当我尝试将时间延长到半小时时会出错。

Here is my code, how do i display the time intervals as 9:30 and 16:30? 这是我的代码,如何将时间间隔显示为9:30和16:30?

var time = new Date().getHours();
var data;
if (time < 10) {
    data = x[0].getElementsByTagName("PREVIOUSCLOSE")[0].childNodes[0].nodeValue;}

else if (time < 16) {
    data = x[0].getElementsByTagName("ASK")[0].childNodes[0].nodeValue;}
else {data = x[0].getElementsByTagName("LASTPRICE")[0].childNodes[0].nodeValue;}

Just use minutes together with hours 只需数分钟和数小时

var date  = new Date();
var hours = date.getHours();
var mins  = date.getMinutes();
var data;

if (hours < 9 || ( hours < 10 && minutes < 30  ) ) {

    data = x[0].getElementsByTagName("PREVIOUSCLOSE")[0].childNodes[0].nodeValue;

} else if (hours < 16 || ( hours < 17 && minutes < 30)) {

    data = x[0].getElementsByTagName("ASK")[0].childNodes[0].nodeValue;

} else {

    data = x[0].getElementsByTagName("LASTPRICE")[0].childNodes[0].nodeValue;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM