0

たとえば、JavaScriptを使用して時間に応じて特定の部分を非表示にするHTMLテーブルを作成しています。

6:30
6:45
7:05

現在の時刻が 6:30 以上の場合、最初のセルは非表示になります。

私がこれを始める方法は次のとおりです。

var now = new Date(); // 日付オブジェクトを作成する
var h = now.getHours(); // 現在の時間を取得する
var m = now.getMinutes(); // 現在の分を取得する

そして後で;

if (h>=6 && m>=30) {
$('table#truetable tr:first').hide();
}

これは機能しません (問題は最後の部分にあると思います)。分数が 30 を超えないため、この (最初の) セルを 7:25 としましょう。他の多くの場合は機能しません。

これを修正できますか? 別の方法で行う必要がありますか?

4

4 に答える 4

2

分単位で比較:

if( h*60+m/*h:m*/ >= 6*60+30/*6:30*/ ){
}
于 2012-06-30T18:42:03.843 に答える
1
var t = new Date()
undefined
t.getHours()
20
t.getHours()>=6
true
h = t.getMinutes()
51
t>=30
true

これは機能します。問題は、時間と分をチェックしていることです。つまり、分が 30 未満の場合は false が返されます。

あなたのifは次のように翻訳されます:

any hour bigger than six whose minutes are also bigger than 30

あなたのif条件は次のとおりです。

if(h>=6 && m>=30 || h>=7)

または数字のみ

if(h*60+m>= 390)
于 2012-06-30T18:53:36.927 に答える
1

最も簡単な方法は、ケースが 6 時の場合を個別に処理することです。

if (h > 6 || (h == 6 && m >= 30)) {
  // Modify DOM
}
于 2012-06-30T18:47:54.633 に答える
1

hh:mmorhh:mm:ss形式の時間を秒に変換する関数を作成しました。以下で見つけることができます:

function hourConvert(str) {
    //this separates the string into an array with two parts, 
    //the first part is the hours, the second the minutes
    //possibly the third part is the seconds
    str = str.split(":"); 

    //multiply the hours and minutes respectively with 3600 and 60
    seconds = str[0] * 3600 + str[1] * 60;

    //if the there were seconds present, also add them in
    if (str.length == 3) seconds = seconds + str[2];

    return seconds;
}

時間を互いに簡単に比較できるようになりました。

if (hourConvert(str) > hourConvert("6:30")) //Do Stuff

実際に見てみましょう: http://jsfiddle.net/TsEdv/1/

于 2012-06-30T18:50:33.060 に答える