Simple comparison between dates

Asked

Viewed 26,625 times

9

I need a simple comparison between javascript dates the end date cannot be less than the start date. I found some scripts on the internet and tried to adapt to mine, but since I know practically nothing, it didn’t work.

Follow the script I’m adapting.

  <script language="javascript">
function checarDatas(){
var form_insere = document.form_insere;

var data_1 = form_insere.datainicial.value;
var data_2 = form_insere.datafinal.value;
var Compara01 = parseInt(data_1.split("/")[2].toString() + data_1.split("/")[1].toString() + data_1.split("/")[0].toString());
var Compara02 = parseInt(data_2.split("/")[2].toString() + data_2.split("/")[1].toString() + data_2.split("/")[0].toString());

if (Compara01 > Compara02) {
   alert("Data não pode ser maior que a data final");
   return false;
}
else {
    return true
}
 }
    </script>

also put onsubmit="return checarDatas()" on the form tag. The goal is when the user enters an initial date (07/05/2014) and then on the final date (06/04/2014) triggers an alert indicating that the dates are wrong and do not let him submit the form until back and correct. Thank you, and I accept any kind of suggestions.

3 answers

13


I believe it is enough to create date objects and compare them.

function checarDatas() {
    var form_insere = document.form_insere;

    var data_1 = new Date(form_insere.datainicial.value);
    var data_2 = new Date(form_insere.datafinal.value);
    if (data_1 > data_2) {
        alert("Data não pode ser maior que a data final");
        return false;
    } else {
        return true
    }
}

These validations can be more or less complex depending on what you want to do. I suggest adding a check in case the dates were not chosen. In its simplest form it would be:

if (!data_1 || !data_2) return false;

Example

  • "You can not accept an answer in 7 minutes" It worked yes... With much less code yet :) Idea: Is there any way he can get a reliable date from somewhere so he can also compare and not put the dates in the past tense? For example: 05/05/2014 yesterday.

  • I imagine there’s a way he could get a date from the user’s watch for this kind of comparison, but there’s a way around it if the user changes the time of their site.

  • 1

    @Vinicius, yes the user-side dates are dependent on the browser and can be changed by the user. Best to use server-side time.

  • because if you put a } Else if (data_1 == data_2) { does not work and always falls on Else

  • @Leocaracciolo two date objects always give different. Ie new Date() == new Date() always gives false.

  • and how will I check for equal dates?

  • if I put >= in the same works

  • @LeoCaracciolo https://answall.com/a/67850/129

  • but there is no input type date

  • @Leocaracciolo input type date always gives a string. Asks a question that gives a help.

Show 5 more comments

5

Complementing:

There is a great library, called Moment js., specially made to deal with dates/times/timezones. Depending on the need for date manipulation in your project, it can become interesting.

An example of comparing dates using Moment.js:

var agora = moment();
var amanha = moment().add('days', 1);

if (agora < amanha) {
    // alguma coisa
}
else {
    // outra coisa
}
  • Interesting, in this project there is a part where are listed the Forms with different start dates. It would be nice to also put something like: Such a thing: Next Saturday, Something else: Two weeks from now. Looks better aesthetically to view the Forms the user wants.

0

Using the Moment library we can make a comparison this way:

let date1 = moment(date_1, "DD/MM/YYYY").format("YYYYMMDD");
let date2 = moment(date_2, "DD/MM/YYYY").format("YYYYMMDD");

if (date2 > date1) {
  return false;
}
return true;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.