How to check if the input date matches today's date?

I have a form input with id 'date_trans'. The format for entering this date (which is a server-side check) can be any of:

  • dd / mm / yyyy
  • dd-mm-yyyy
  • yyyy-mm-dd
  • yyyy / mm / dd

However, before submitting the form, I would like to check if the date_trans field has a date equal to today's date. It’s good if the specified date is the client date (i.e. Uses js), since I am running a double check on the server.

I completely lost how to perform date matching in jQuery or just plain old javascript. If this helps, I use jquery datepicker

+42
javascript jquery
Nov 21 '11 at 17:02
source share
9 answers

Have you watched datejs ? (using the appropriate globalization package to properly analyze dd / mm / yyyy)

Just pass the date to your parse() method and compare with today's date.

-four
Nov 21 '11 at 17:10
source share

A simple date comparison in pure JS should be sufficient:

 // Create date from input value var inputDate = new Date("11/21/2011"); // Get today date var todaysDate = new Date(); // call setHours to take the time out of the comparison if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) { // Date equals today date } 

Here's a working JSFiddle .

+129
Nov 21 '11 at 17:08
source share

for completeness taken from this solution :

You can use toDateString:

 var today = new Date(); var isToday = (today.toDateString() == otherDate.toDateString()); 

there are no dependencies in the library and looks more understandable than the setHours () method shown in the previous answer, imho

+53
Oct 08
source share

Try using moment.js

 moment('dd/mm/yyyy').isSame(Date.now(), 'day'); 

You can replace the string "day" with "year, month, minute" if you want.

+17
Jan 12 '14 at 13:02
source share
 function sameDay( d1, d2 ){ return d1.getUTCFullYear() == d2.getUTCFullYear() && d1.getUTCMonth() == d2.getUTCMonth() && d1.getUTCDate() == d2.getUTCDate(); } if (sameDay( new Date(userString), new Date)){ // ... } 

Using UTC * methods ensures that two equivalent days in different time zones that coincide with the same global day are the same. (Not necessarily if you parse both dates directly, but think twice.)

+6
Nov 21 '11 at 17:07
source share

Just use the following code in javaScript:

 if(new Date(hireDate).getTime() > new Date().getTime()) { //Date greater than today date } 

Modify the condition as per your requirement. Here is one link to compare compare in java script

+4
Jul 02 '13 at 9:37 on
source share

Date.js is a handy library for managing date formatting. This may help in this situation.

0
Nov 21 '11 at 17:08
source share

The following solution compares an integer number of time divided by the values ​​of hours, minutes, seconds, milliseconds.

 var reducedToDay = function(date){return ~~(date.getTime()/(1000*60*60*24));}; return reducedToDay(date1) == reducedToDay(date2) 

Tilda converts the result of division (see this article about whole divisions )

0
25 Oct '15 at 2:37
source share
 TodayDate = new Date(); if (TodayDate > AnotherDate) {} else{} 

<= also works, although with =, it may need to coincide with milliseconds.

-one
Oct 13 '15 at 10:10
source share



All Articles