Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if one date is before another date using JavaScript\JQuery?

I am absolutely new in JavaScript development and I have to perform the following task: I have 2 input tag containing 2 string representing date in the form 01/12/2014 (DAY/MONTH/YEAR). I use this input tag to search objects that have a date field that is among these dates.

<input type="hidden" size="9" class="impPrfTot" readonly="readonly"                                            onchange="cambioDataDa(this.value)"                                            name="dataDa" id="datada" value="<%=request.getSession().getAttribute("dataDa")%>"                                            style="font-size: 11px;color: #000000;">  <input type="hidden" size="9" class="impPrfTot" readonly="readonly"                                            onchange="cambioDataA(this.value); checkData(this.value)"                                            name="dataA" id="dataa" value="<%=request.getSession().getAttribute("dataA")%>"                                            style="font-size: 11px;color: #000000;">  <button class="dataDadataAButton" name="submitdataDadataA" onclick="sottomettiFormDataDaA(this)">Cerca</button> 

And finally I have a button used to submit this form using this JavaScript:

function sottomettiFormDataDaA(obj) {     document.getElementById('dataDaAForm').submit(); } 

What I need is to prevent that the value inside dataA (in English language dateTo) input is previous that dataDa value (in English language dateFrom).

I am trying to do something like this:

  1. The JavaScript is called at the onchange event on the change of a data and take the dataA string (tha represent the dateTo date) and check if it is previous of the dataA (the dateTo date).

  2. If the previous check is true the date range is invalid so the script show an error popup message and disallow the submit of the form having id="dataDaAForm"

    function checkData(dataA) {     dataDa = document.getElementById('dataDa').value;      if(dataDa > dataA){         // SHOW A POPUP ERROR MESSAGE         // DISALLOW THE FORM SUBMIT     } } 

Bue I have really not idea about complete this JavaScript and I don't know it exist better solution to achieve this task.

like image 932
AndreaNobili Avatar asked Dec 05 '14 13:12

AndreaNobili


People also ask

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.

How do I know if a date is between two dates?

To check if a date is between two dates: Use the Date() constructor to convert the dates to Date objects. Check if the date is greater than the start date and less than the end date. If both conditions are met, the date is between the two dates.

How do you check if a date has passed JavaScript?

To check if a date is in the past: Use the Date() constructor to get the current date. Optionally set the time of the current date to midnight. Check if the date is less than the current date.

How to check if one date is between two dates in JavaScript?

How to check if one date is between two dates in JavaScript ? The task is to determine if the given date is in between the given 2 dates or not? Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach, we will use .split () method and the new Date () constructor.

How to get date and time in JavaScript?

Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach, we will use .split () method and the new Date () constructor. And in the second approach we will use the .getTime () method with the new Date () constructor.

How to check if the passed in date is before today's date?

We created a reusable function that checks if the passed in date is before today's date. The first thing we did in the function is use the Date () constructor to get the current date. The setHours method takes the hours, minutes, seconds and milliseconds as parameters and changes the values for the given Date instance.

How to get day month and year in an array using JavaScript?

In the first approach, we will use .split () method and the new Date () constructor. And in the second approach we will use the .getTime () method with the new Date () constructor. Approach 1: Use .split () method to split the date on “/” to get the day, month and year in an array.


1 Answers

Very simple, Date instance can be compared directly.

function compareTime(time1, time2) {     return new Date(time1) > new Date(time2); // true if time1 is later } 

When you compare two Date instance, or minus one another, the valueOf method will be called internally, which convert the instance to timestamp (millisecond accurate).

like image 123
Leo Avatar answered Sep 26 '22 12:09

Leo