title: Comparing two dates with javascript or datejs (date difference)
body: <p>I am trying to compare two dates which are in Finnish time form like this: dd.mm.YYYY or d.m.YYYY or dd.m.YYYY or d.mm.YYYY.</p>
  
  <p>I am having a hard time finding out how to do this, my current code won't work.</p>
  
  <pre><code>&lt;script src="inc/date-fi-FI.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      function parseDate() {
          var date = $('#date').val();
          var parsedDate = Date.parse(date);
          alert('Parsed date: '+parsedDate);
      }
      function jämförMedIdag (datum) {
      if (datum == null || datum == "") {
          alert('Inget datum!');
          return;
      }
      /*resultat = Date.compare(Datum1,Datum2);
      alert(resultat); */
      var datum = Date.parse(datum);
      var dagar = datum.getDate();
      var månader = datum.getMonth();
      var år = datum.getYear();
      var nyttDatum = new Date();
      nyttDatum.setFullYear(år,månader,dagar);
      var idag = new Date();
  
      if(nyttDatum&gt;idag) {
          var svar = nyttDatum - idag;
          svar = svar.toString("dd.MM.yyyy");
          alert(svar);
          return(svar);
      } else {
          var svar = idag - nyttDatum;
          svar = svar.toString("dd.MM.yyyy");
          alert(svar);
          return(svar);
      }
  }    
  &lt;/script&gt;
  </code></pre>
  
  <p>This code will try to calculate the difference between two dates, one of them being today. No success lolz.</p>
  
  <p>Thanks in advance!</p>
  
  <p>My final code (thanks RobG!):</p>
  
  <pre><code>function dateDiff(a,b,format) {
      var milliseconds = toDate(a) - toDate(b);
      var days = milliseconds / 86400000;
      var hours = milliseconds / 3600000;
      var weeks = milliseconds / 604800000;
      var months = milliseconds / 2628000000;
      var years = milliseconds / 31557600000;
      if (format == "h") {
          return Math.round(hours);
      }
      if (format == "d") {
          return Math.round(days);
      }
      if (format == "w") {
          return Math.round(weeks);
      }
      if (format == "m") {
          return Math.round(months);
      }
      if (format == "y") {
          return Math.round(years);
      }
  }
  </code></pre>
  
  <p>It is not fully accurate, but very close. I ended up adding some addons to it to calculate in day week month year or hour, anyone can freely copy and use this code.</p>