html - Using Javascript to subtract two dates given from form entries -


i'm trying use javascript subtract 2 dates. date values come form fields. i'm using formidable pro on wordpress site form.

the form 1 allow business pay taxes. taxes due on 20th of each month. business may pay taxes late, @ beginning of form specify month paying taxes for.

the first date current date. it's populated form field using formidable short code in format mm/dd/yyyy.

the second date calculated from entries year , month. values concatenated "20" form full date value 20th of month paying taxes.

if paying taxes on or before 20th, there no penalty. if it's after 20th, there's penalty. want subtract dates , determine whether payment late based on difference being greater zero--and difference value being populated form field.

this code i've been using. you'll see commented out 1 method of calculating , returning difference value form.

with code below, script returns "nan" error difference field.

why? how fix error , have report difference? need know if negative or >= zero.

<script type="text/javascript"> jquery(document).ready(function($){ $('#field_21nfg, #field_o1ns5').change(function(){   var year = $("#field_21nfg").val();   var month = $("#field_o1ns5").val();    $("#field_tm25b").val(month+'/'+'20'+'/'+year);  // var due = $("#field_tm25b5").val(); // var today = $("#field_1pjvu").val(); // // var ddue = new date('due'); // var dtoday = new date('today'); // var diff = math.floor(ddue - dtoday);  // $("#field_smmyr").val(diff);  var oneday = 24*60*60*1000;  var today_str = $("#field_1pjvu").val(); // e.g., "mm/dd/yyyy"; var today_dt = new date(parseint(today_str.substring(6), 10),        // year                   parseint(today_str.substring(0, 2), 10) - 1, // month (0-11)                   parseint(today_str.substring(3, 5), 10));    // day  var due_str = $("#field_tm25b5").val(); // e.g., "mm/dd/yyyy"; var due_dt = new date(parseint(due_str.substring(6), 10),        // year                   parseint(due_str.substring(0, 2), 10) - 1, // month (0-11)                   parseint(due_str.substring(3, 5), 10));    // day  //var diff = math.floor(due_dt-today_dt); var diffdays = math.round(math.abs((today_dt.gettime() - due_dt.gettime())/(oneday)));  $("#field_smmyr").val(diffdays);   }); }); </script> 

"1pjvu" key of today's date field

"21nfg" year value field of billing period given user.

"o1ns5" month value field of billing period field given user.

"tm25b" concatenated due date: 20th of month given in above field.

"smmyr" key of difference field.

update april 19 2016

thank help. if can't tell, don't know js hacking way along go. after fumbling, got work. here's final code:

<script type="text/javascript"> jquery(document).ready(function($){ $('#field_21nfg, #field_o1ns5').change(function(){    var year = $("#field_21nfg").val(); //collect value filing period year form field   var month = $("#field_o1ns5").val();//collect value filing period month form field   var day = 20; //due date filing tax return   var duedate = month+'/'+day+'/'+year;   $("#field_tm25b").val(duedate); //populate "due date" form field due date generated above  function parsemdy(s) {   var b = s.split(/\d/);   return new date(b[2], b[0]-1, b[1]); }  var today = $("#field_1pjvu").val(); //collect value today's date form field  if (duedate > today) { $("#field_smmyr").val(1); //returns true/1 on or before due date } else { $("#field_smmyr").val(0); //returns false/0 after due date }  }); }); </script> 

you making life more difficult needs be. parse date string in m/d/y format, use simple function 1 below.

if want compare dates using < or > whole days, can compare them. though relational comparison operators compare dates numbers, == , === operators don't, clearer explicitly compare time values.

function parsemdy(s) {    var b = s.split(/\d/);    return new date(b[2], b[0]-1, b[1]);  }    var = '2/29/2016';  var b = '12/31/2015';    document.write(parsemdy(a) + '<br>' + parsemdy(b) + '<br>');      document.write('is ' + + ' before ' + b + '? ' +                (parsemdy(a).gettime() < parsemdy(b).gettime())  );

and if want compare "today", don't forget set hours zero:

var today = new date(); today.sethours(0,0,0,0); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -