2つの日付の値を比較するJavaScriptプログラム

Javascript

例:2つの日付の値を比較する

// program to compare value of two dates
// create two dates
const d1 = new Date();
const d2 = new Date();

// comparisons
const compare1 = d1 < d2;
console.log(compare1);

const compare2 = d1 > d2;
console.log(compare2);

const compare3 = d1 <= d2;
console.log(compare3);

const compare4 = d1 >= d2;
console.log(compare4);

const compare5 = d1.getTime() === d2.getTime();
console.log(compare5);

const compare6 = d1.getTime() !== d2.getTime();
console.log(compare6);

出力

false
false
true
true
true
false

上記の例では、 new Date() コンストラクターは、日付オブジェクトを作成するために使用されます。

ザ・ new Date() 現在の日付と時刻を示します。

const d1 = new Date();
console.log(d1); // Fri Aug 28 2020 09:19:40 GMT+0545 (+0545)

次に、比較演算子を使用して、これら2つの日付を直接比較できます。 ><<=、または >=

ただし、次のような比較演算子を使用するには ==!====、または !==、使用する必要があります date.getTime()

ザ・ getTime() メソッドは、の午前0時からのミリ秒数を返します。 1970年1月1日(EcmaScriptエポック) 指定された日付に。

const d1 = new Date().getTime();
console.log(d1); // 1598585951699



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です