JavaScript: Splitting and joining dates in mm/dd/yyyy format -
i'm having bit of difficulty comparing 2 dates entered in mm/dd/yyyy format. have following code written try convert them yyyy-mm-dd format, , display error div if start date greater end date:
for(i=0; i<=9; i++) { datefrom+i = document.getelementbyid('projdatefrom'+i).value; dateto+i = document.getelementbyid('projdateto+i').value; datefrom+i = datefrom+i.split("/").reverse().join("-"); dateto+i = dateto+i.split("/").reverse().join("-"); if(datefrom+i > dateto+i) { document.getelementbyid('dateerror').style.display = ''; return false; }
when enter dates on form in mm/dd/yyyy format, , deliberately enter projdatefrom greater projdateto, nothing happens. missing?
moment.js fantastic date tool: http://momentjs.com/docs/#/displaying/difference/
here's fiddle take date format, convert proper javascript object wrapped many convenient date methods.
example fiddle: http://jsfiddle.net/cfa2e/3
var diff = end.diff(start, 'days');
where end
date compare against start
Comments
Post a Comment