Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare string with today's date in JavaScript

Tags:

I've got a string from an input field which I use for date with a format like this 25-02-2013. Now I want to compare the string with today's date. I want to know if the string is older or newer then today's date.

Any suggestions?

like image 719
Leon van der Veen Avatar asked Feb 25 '13 09:02

Leon van der Veen


People also ask

Can you compare a string and a date?

To compare two dates, you can use either toString() or valueOf() . The toString() method converts the date into an ISO date string, and the valueOf() method converts the date into milliseconds since the epoch.

How do I compare today's date with different dates in typescript?

Call the getTime() method on each date to get a timestamp. Compare the timestamp of the dates. If a date's timestamp is greater than another's, then that date comes after.

How do you compare two date objects in JavaScript?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.


Video Answer


1 Answers

    <script type="text/javascript">  var q = new Date(); var m = q.getMonth()+1; var d = q.getDay(); var y = q.getFullYear();  var date = new Date(y,m,d);  mydate=new Date('2011-04-11'); console.log(date); console.log(mydate)  if(date>mydate) {     alert("greater"); } else {     alert("smaller") }   </script> 
like image 88
polin Avatar answered Oct 13 '22 02:10

polin