0

i'm beginnig with javascript and my case seems really complicated for me ! i'm trying to compare today's date to "DateFinReele" date my "DateFinReele" date hs to be smaller than today's date i have another control on the "DateDebut" , "DateFinPrevue" and "DateFinReele" the "DateFinPrevue" and "DateFinReele" have to be smaller than the "DateDebut" when i remove my function checkDateInpuWithTodays() the other control works very well but when i add it nothing is working (even the other control !!!)

script >
  $(document).ready(function() {
    function DateDebutChange() {
      var DateDebutDate = DateDebut.value(),
        DateFinPrevueDate = DateFinPrevue.value(),
        DateFinReeleDate = DateFinReele.value();
      if (DateDebutDate) {
        DateDebutDate = new Date(DateDebutDate);
        DateDebutDate.setDate(DateDebutDate.getDate());
        DateFinPrevue.min(DateDebutDate);
        DateFinReele.min(DateDebutDate);
      } else if (DateFinPrevueDate) {
        DateDebut.max(new Date(DateFinPrevueDate)); <
      } else if (DateFinReeleDate) {
        DateDebut.max(new Date(DateFinReeleDate));
      } else {
        DateFinPrevueDate = new Date();
        DateFinReeleDate = new Date();
        DateDebut.max(DateFinPrevueDate);
        DateFinPrevue.min(DateFinPrevueDate);
      }
    }

    function DateFinReeleChange() {
      var DateFinReeleDate = DateFinReele.value(),
        DateDebutDate = DateDebut.value(),
        DateFinPrevueDate = DateFinPrevue.value();
      if (DateFinReeleDate) {
        DateFinReeleDate = new Date(DateFinReeleDate),
          DateFinPrevueDate = DateFinPrevue.value(),
          DateDebutDate = DateDebut.value();
      } else if (DateDebutDate) {
        DateFinPrevue.min(new Date(DateDebutDate)),
          DateFinReele.min(new Date(DateDebutDate));
      } else {
        DateFinPrevueDate = new Date();
        DateDebut.max(DateFinPrevueDate)
        DateDebut.max(DateFinReeleDate)
        DateFinPrevue.min(DateFinPrevueDate);
        DateFinReele.min(DateFinReeleDate)
      }
    }

    function DateFinPrevueChange() {
      var DateFinPrevueDate = DateFinPrevue.value(),
        DateDebutDate = DateDebut.value();
      if (DateFinPrevueDate) {
        DateFinPrevueDate = new Date(DateFinPrevueDate);
        DateFinPrevueDate.setDate(DateFinPrevueDate.getDate());
        DateDebut.max(DateFinPrevueDate);
      } else if (DateDebutDate) {
        DateFinPrevue.min(new Date(DateDebutDate));
      } else {
        DateFinPrevueDate = new Date();
        DateDebut.max(DateFinPrevueDate)
        DateFinPrevue.min(DateFinPrevueDate);
      }
    }

    function checkDateInpuWithTodays() {
      var x = new Date();
      var DateFinReeleDate = DateFinReele.value();
      if (DateFinReeleDate.value > x.valueOf()) {
        alert("La date de fin du projet doit être inférieure à celle d'aujourd'hui !")
        DateFinReele.value = null;
        DateFinReele.ready;
      }
    }
    var DateDebut = $("#DateDebut").kendoDatePicker({
      change: DateDebutChange
    }).data("kendoDatePicker");
    var DateFinPrevue = $("#DateFinPrevue").kendoDatePicker({
      change: DateFinPrevueChange
    }).data("kendoDatePicker");
    var DateFinReele = $("#DateFinReele").kendoDatePicker({
      change: DateFinReeleChange
    }).data("kendoDatePicker");
    DateDebut.max(DateFinPrevue.value());
    DateFinPrevue.min(DateDebut.value());
    DateFinReele.min(DateDebut.value());
    var DateFinReele = $("DateFinReele").kendoDatePicker({
      change: checkDateInpuWithTodays
    }).data("kendoDatePicker");
    DateFinReele.max(new Date().valueOf());
  }); < /script>

can anybody help me

KhaoulaAtallah
  • 759
  • 1
  • 6
  • 16
  • posting a whole bunch of asp.net code wont help that much. What is the javascript question? – Regis Portalez Apr 21 '16 at 08:18
  • you're right ! sorry ! i updated my post , take a look please – KhaoulaAtallah Apr 21 '16 at 08:24
  • I understand that your function "checkDateInpuWithTodays" not only doesn't work, but breaks everything. Do you have any error in console? – Regis Portalez Apr 21 '16 at 08:28
  • exactly !! it breaks my others codes ! but i don't have any error ! it adds the data to the database and everything works normally – KhaoulaAtallah Apr 21 '16 at 08:31
  • I'm sorry, but the more I look at your code, the less it seems reasonable for it to work. You should really refactor it somehow. For example, at the end, you define and initialize twice the variable "DateFinReele", and subscribe to change event with two functions (as far as i understand) – Regis Portalez Apr 21 '16 at 08:42
  • actually i'm beginner in js , so that's why my code is not reasonable – KhaoulaAtallah Apr 21 '16 at 08:55

2 Answers2

2

You can actually compare JavaScript dates very easily just using normal operators.

Each Date object is really just a representation of the number of milliseconds since Jan 1, 1970. You can see a Date's time value using the getTime method.

var april212016 = new Date('2016-04-21');
console.log(april212016.getTime()); // 1461196800000

var wayWayBack = new Date('1970-01-01');
console.log(wayWayBack.getTime()); // 0

As such, when comparing dates, behind the scenes it just compares those numbers:

var d1 = new Date('2016-03-01');
var d2 = new Date('2016-01-01');

console.log(d1 > d2); // true
console.log(d2 < d1); // true
console.log(d1 == d2); // false
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • var today = new Date('2016-04-21'); !!!! should i update my code everyday to set the today's date ? – KhaoulaAtallah Apr 21 '16 at 08:26
  • This, however If you need better date conversion handling I'd recommend Moment.js – Jay Apr 21 '16 at 08:27
  • 1
    @kokomoi no, I was just showing an example. You can get the current date/time just by doing `var now = new Date()` - I updated the code above to make that clear. – Matthew Herbst Apr 21 '16 at 08:29
  • http://stackoverflow.com/questions/12409299/how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-i – Jay Apr 21 '16 at 08:30
  • Yeah, I agree @Jay, Moment.js is awesome! Though probably overkill if all OP is doing is simple > | < comparison. – Matthew Herbst Apr 21 '16 at 08:31
  • maybe Moment.js is great but i took a look and it seems really complicated for a beginner , thanks anyway :) – KhaoulaAtallah Apr 21 '16 at 08:57
0

okeyyy ! thanks everyone for your help and your time ! i finally solved the problem with kendo validator

i just changed this code

var DateFinReele = $("#DateFinReele").kendoDatePicker({
  change: DateFinReeleChange,
  max: new Date()
}).data("kendoDatePicker");
KhaoulaAtallah
  • 759
  • 1
  • 6
  • 16