如果我在第一个文本框中输入日期,我需要在第二个文本框中获取持续时间当前日期,我做的一些工作很好,只有与dd/mm/yyyy格式,但我需要在mm/dd/yyyy格式输入日期。我的工作代码将在这里,请帮助我
<input type="text" name"mydate" id="mydate" placeholder="mm/dd/yyyy">
<input type="text" name="duration" id="duration">
<script>
function getYearFn()
{
from = $("#mydate").val().split("/");
var fromdate = new Date(from[2], (from[1]-1), from[0]);
var today = new Date();
var year1 = fromdate.getFullYear();
var year2 = today.getFullYear();
var yeardiff = year2 - year1;
var month1 = fromdate.getMonth()+1;
var month2 = today.getMonth()+1;
var monthdiff = (month1 - month2)*-1;
$('#duration').val(yeardiff +" " +"Years"+" "+monthdiff*-1 +""+"Months" )
}
</script>
您只需要交换日期创建中的参数。
而不是
var fromdate = new Date(from[2], (from[1]-1), from[0]);
使用var fromdate = new Date(from[2], (from[0]-1), from[1]);