1

JavaScript で 2 つの日付を比較しようとしていますが、このサイトで正確な答えをまだ見つけることができませんでした。

私は JavaScript を使い始めたばかりで、現在の日付\時刻を取得することは、私が思っていた単純な 1 行のコードではないという印象を受けました。三項演算子を使用できると思っていましたが、そうではないかもしれません。私が間違っていることを願っていますが、ここに私のジレンマがあります。

2 つの日付を比較したい。date1 が date2 より前の場合は、何かを行います。そうでない場合は、他のことをします。

  • date1 : 現在の日時です。つまり、私のシステムクロック
  • date2 : 事前定義された値です。私はすでに Web ページにこの値を持っています: ${expiredDateTime.format("ddMyyyyhm") 日付 2 は文字列だと思いますが、jquery の専門家ではありません。

これに関するどんな助けも素晴らしいでしょう。

4

4 に答える 4

4

以下のコードを試してください。

var user="12/11/2012/5/30";
var arrdt= user.split("/");
var userdt = new Date(arrdt[2], arrdt[1] - 1, arrdt[0],arrdt[3],arrdt[4]);
var currdt = new Date();
if (userdt < currdt) {
alert("userdate is before current date");
}else{
alert("userdate is after current date");
}
于 2013-02-01T12:05:38.800 に答える
2

JavaScript で日付を比較する最も簡単な方法は、最初にそれを Date オブジェクトに変換してから、これらの日付オブジェクトを比較することです。

以下に、3 つの機能を持つオブジェクトを示します。

日付.compare(a,b)

数値を返します:

-1 a < b の場合 0 a = b の場合 1 a > b の場合 NaN a または b が不正な日付の場合 date.inRange (d,start,end)

ブール値または NaN を返します。

d が start と end の間 (両端を含む) の場合は true、d が start の前または end の後の場合は false。1 つ以上の日付が不正な場合は NaN。日付.変換

入力を日付オブジェクトに変換するために他の関数によって使用されます。入力は

date-object : 入力がそのまま返されます。配列: [年、月、日] として解釈されます。注 月は 0 ~ 11 です。数値: 1970 年 1 月 1 日 (タイムスタンプ) からのミリ秒数として解釈されます。オブジェクト: 年、月、および日付の属性を持つオブジェクトとして解釈されます。注 月は 0 ~ 11 です。

 // Source: http://stackoverflow.com/questions/497790
    var dates = {
        convert:function(d) {
            // Converts the date in d to a date-object. The input can be:
            //   a date object: returned without modification
            //  an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
            //   a number     : Interpreted as number of milliseconds
            //                  since 1 Jan 1970 (a timestamp) 
            //   a string     : Any format supported by the javascript engine, like
            //                  "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
            //  an object     : Interpreted as an object with year, month and date
            //                  attributes.  **NOTE** month is 0-11.
            return (
                d.constructor === Date ? d :
                d.constructor === Array ? new Date(d[0],d[1],d[2]) :
                d.constructor === Number ? new Date(d) :
                d.constructor === String ? new Date(d) :
                typeof d === "object" ? new Date(d.year,d.month,d.date) :
                NaN
            );
        },
        compare:function(a,b) {
            // Compare two dates (could be of any type supported by the convert
            // function above) and returns:
            //  -1 : if a < b
            //   0 : if a = b
            //   1 : if a > b
            // NaN : if a or b is an illegal date
            // NOTE: The code inside isFinite does an assignment (=).
            return (
                isFinite(a=this.convert(a).valueOf()) &&
                isFinite(b=this.convert(b).valueOf()) ?
                (a>b)-(a<b) :
                NaN
            );
        },
        inRange:function(d,start,end) {
            // Checks if date in d is between dates in start and end.
            // Returns a boolean or NaN:
            //    true  : if d is between start and end (inclusive)
            //    false : if d is before start or after end
            //    NaN   : if one or more of the dates is illegal.
            // NOTE: The code inside isFinite does an assignment (=).
           return (
                isFinite(d=this.convert(d).valueOf()) &&
                isFinite(start=this.convert(start).valueOf()) &&
                isFinite(end=this.convert(end).valueOf()) ?
                start <= d && d <= end :
                NaN
            );
        }
    }
于 2013-02-01T11:56:59.617 に答える
0

リンクの重複の可能性

if (date1.getTime() > date2.getTime()) {
    alert("The first date is after the second date!");
}

Date オブジェクトへの参照

于 2013-02-01T11:55:52.050 に答える
0

現在の月、年、日付を取得するためのコードの下を試してください

var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
于 2013-02-01T11:57:21.537 に答える