logo

Javascript Compara datele

În secțiunea anterioară, am discutat despre metodele datei, precum și despre constructori.

Aici, cu ajutorul acelor metode, vom învăța să comparăm datele.

Practic, există diferite moduri prin care putem compara datele, cum ar fi:

  1. Comparând două întâlniri una cu alta.
  2. Compararea datei cu ora.
  3. Compararea datelor folosind getTime()

Comparând două întâlniri una cu alta

Exemplu:

 Comparing Dates<br> function compare() { var d1=new Date(&apos;2020-01-23&apos;); //yyyy-mm-dd var d2=new Date(&apos;2020-01-21&apos;); //yyyy-mm-dd if(d1&gt;d2) { document.write(&apos;True, First date is greater than second date&apos;); } else if(d1<d2) { document.write('false, second date is smaller than the first'); } else document.write('both are same and equal'); compare(); invoking compare() < pre> <span> Test it Now </span> <h2>Comparing date with time</h2> <p> <strong>Example 1:</strong> Comparing different dates with different timings</p> <pre> Comparing Date and time<br> var d1=new Date(&apos;Apr 17, 2019 12:10:10&apos;); //mm dd, yyyy hh:mm:ss var d2=new Date(&apos;Dec 1, 2019 12:10:30&apos;); //mm dd, yyyy hh:mm:ss if(d1&gt;d2) { document.write(&apos;False, d1 date and time is smaller than d2 date and time&apos;); } else if(d1<d2) { document.write('true, d2 is greater in terms of both time and date'); } else document.write('both date are same equal'); < pre> <span> Test it Now </span> <p> <strong>Example2:</strong> Comparing same dates with disimilar timings</p> <pre> Comparing same date but different time<br> var d1=new Date(&apos;2018-01-10, 12:10:10&apos;); //yyyy-mm-dd hh:mm:ss var d2=new Date(&apos;2018-01-10, 12:10:50&apos;); //yyyy-mm-dd hh:mm:ss if(d1&gt;d2) { document.write(&apos;False, d1 &amp; d2 dates are same but d2 time is greater than d1 time&apos;); } else if(d1<d2) { document.write('true, d2 time is greater than d1 time.'); } else document.write('both date and are same equal'); < pre> <span> Test it Now </span> <h2>Comparing date with getTime()</h2> <p>A better approach to make comparison between dates is to use <strong>getTime()</strong> function. This function lets converting date into numeric value to directly compare them. </p> <p> <strong>Example1:</strong> Comparing current date and time with a given date and time.</p> <pre> Comparing Dates<br> var d1=new Date(&apos;2019-10-10, 10:10:10&apos;); //yyyy-mm-dd hh:mm:ss var currentdate=new Date(); //fetch the current date value if(d1.getTime()currentdate.getTime()) { document.write(&apos;False&apos;); } else { document.write(&apos;True, equal&apos;); } </pre> <span> Test it Now </span> <p> <strong>Example2:</strong> Comparing two different dates with different timings.</p> <pre> Comparing Dates<br> var d1=new Date(&apos;2019-10-10, 10:10:10&apos;); var d2=new Date(&apos;2019-11-02, 14:19:05&apos;); if(d1.getTime()d2.getTime()) { document.write(&apos;False, d2 date and time are greater than d1&apos;); } else { document.write(&apos;True, d1 and d2 have same time and date&apos;); } </pre> <span> Test it Now </span> <p>Thus, we can compare dates in many possible ways.</p> <h2>Changing Date Format</h2> <p>We can also change or set the format through JavaScript code. The function getFullYear(), GetMonth(), and getDate() allows to set the format of date accordingly.</p> <p> <strong>Example1:</strong> Changing the date format to &apos;yyyy-mm-dd&apos;.</p> <pre> <h3>Changing date format</h3><br> var current_date=new Date(); //fetches current date var set_to=current_date.getFullYear()+&apos;-&apos;+(current_date.getMonth()+1)+&apos;-&apos;+current_date.getDate(); document.write(&apos;The format followed is yyyy-dd-mm: &apos;+set_to); </pre> <span> Test it Now </span> <p>We can also set the date and time format according to our need.</p> <p> <strong>Example2:</strong> Changing the datetime format to &apos;yyyy-dd-mm hh:mm:ss&apos;.</p> <pre> <h3>Changing date format</h3><br> var current_datetime=new Date(); //fetches current date and time var set_to=current_datetime.getFullYear()+&apos;-&apos;+(current_datetime.getMonth()+1)+&apos;-&apos;+current_datetime.getDate()+&apos; &apos;+current_datetime.getHours()+&apos;:&apos;+current_datetime.getMinutes()+&apos;:&apos;+current_datetime.getSeconds(); document.write(&apos;The format followed is yyyy-dd-mm hh:mm:ss : &apos;+set_to); </pre> <span> Test it Now </span> <hr></d2)></pre></d2)></pre></d2)>
Testează-l acum

Exemplul 2: Comparând două date diferite cu momente diferite.

 Comparing Dates<br> var d1=new Date(&apos;2019-10-10, 10:10:10&apos;); var d2=new Date(&apos;2019-11-02, 14:19:05&apos;); if(d1.getTime()d2.getTime()) { document.write(&apos;False, d2 date and time are greater than d1&apos;); } else { document.write(&apos;True, d1 and d2 have same time and date&apos;); } 
Testează-l acum

Astfel, putem compara datele în multe moduri posibile.

Schimbarea formatului datei

De asemenea, putem schimba sau seta formatul prin codul JavaScript. Funcția getFullYear(), GetMonth() și getDate() vă permite să setați formatul datei corespunzător.

Exemplul 1: Schimbarea formatului datei în „aaaa-mm-zz”.

 <h3>Changing date format</h3><br> var current_date=new Date(); //fetches current date var set_to=current_date.getFullYear()+&apos;-&apos;+(current_date.getMonth()+1)+&apos;-&apos;+current_date.getDate(); document.write(&apos;The format followed is yyyy-dd-mm: &apos;+set_to); 
Testează-l acum

De asemenea, putem seta formatul de dată și oră în funcție de nevoile noastre.

Exemplul 2: Schimbarea formatului dată și oră în „aaaa-zz-mm hh:mm:ss”.

 <h3>Changing date format</h3><br> var current_datetime=new Date(); //fetches current date and time var set_to=current_datetime.getFullYear()+&apos;-&apos;+(current_datetime.getMonth()+1)+&apos;-&apos;+current_datetime.getDate()+&apos; &apos;+current_datetime.getHours()+&apos;:&apos;+current_datetime.getMinutes()+&apos;:&apos;+current_datetime.getSeconds(); document.write(&apos;The format followed is yyyy-dd-mm hh:mm:ss : &apos;+set_to); 
Testează-l acum