この記事では、JSの時間比較の簡単な方法について説明します。次のように、参照のために共有してください。
//時間比較(yyyy-mm-dd)関数比較(startdate、enddate){var arrstart = startdate.split( " - "); var starttime = new Date(arrstart [0]、arrstart [1]、arrstart [2]); var starttimes = starttime.getTime(); var arrend = enddate.split( " - "); var endtime = new Date(Arrend [0]、Arrend [1]、Arrend [2]); var endtimes = endtime.getTime(); if(endtimes <starttimes){alert( "終了時間は開始時間よりも短くできない"); falseを返します。 } return true;} //時間比較(yyyy-mm-dd hh:mm:ss)function comparetime(starttime、endtime){var starttimes = starttime.substring(0、10).split( ' - '); var endtimes = endtime.substring(0、10).split( ' - '); starttime = starttimes [1] + ' - ' + starttimes [2] + ' - ' + starttimes [0] + '' + starttime.substring(10、19); endtime = endtimes [1] + ' - ' + endtimes [2] + ' - ' + endtimes [0] + '' + endtime.substring(10、19); var thisresult =(date.parse(endtime) - date.parse(starttime)) / 3600 /1000; if(thisresult <0){alert( "終了時間はtarttimeよりも少ない!"); } else if(thisresult> 0){alert( "終了時間はtarttimeよりも大きい!"); } else if(thisresult == 0){alert( "endtime equals tarttime!"); } else {return '例外'; }}PS:JavaScriptの時間と日付の操作に興味がある友人は、このサイトのオンラインツールを参照することもできます。
UNIXタイムスタンプ変換ツール:
http://tools.vevb.com/code/unixtime
世界中のオンライン時間調査:
http://tools.vevb.com/zhuanhuanqi/worldtime
オンライン永久カレンダー:
http://tools.vevb.com/bianmin/wannianli
Web永久カレンダー:
http://tools.vevb.com/bianmin/webwannianli
For more information about JavaScript, please check this site's special topics: "Summary of JavaScript Time and Date Operation Skills", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Errors and Debugging Skills", "Summary of JavaScript Data Structures and Algorithm Skills", 「JavaScriptトラバーサルアルゴリズムとスキルの概要」および「JavaScript数学的操作の使用の概要」
この記事がみんなのJavaScriptプログラミングに役立つことを願っています。