在我的程序中,我试图显示今天的日期减去1年的例子今天日期(29-4-2016)我想要结果(29-4-2015)。我已经尝试过以下代码,请帮助我。
<div id="date-picker" class="input-group date">
<input class="mdl-textfield__input cDate" name="fromDate" type="text" disabled="disabled" placeholder=" dd/mm/yyyy" />
</div>
<script>
$("#date-picker").datepicker().datepicker("setDate", "0");
</script>
答案 0 :(得分:0)
试试这个。
var d = new Date();
d.setFullYear(d.getFullYear() - 1);
$("#date-picker input").datepicker({
format: 'dd/mm/yyyy'
}).datepicker("setDate", d);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.9.2/themes/black-tie/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<div id="date-picker" class="input-group date">
<input class="mdl-textfield__input cDate" name="fromDate" type="text" placeholder=" dd/mm/yyyy" />
</div>
&#13;
答案 1 :(得分:0)
Hello您可以根据需要使用JS代码
<script>
function convertDate(inputFormat)
{
function pad(s) { return (s < 10) ? '0' + s : s; }
var d = new Date(inputFormat);
return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
} // This is a Fuction For Convert Your Date into dd/mm/yyyy Format
var today = new Date(); // Eg. Wed Apr 29 2016 17:07:18 GMT+0530 (India Standard Time)
var a = convertDate(today); // It Will Convert Your Date By calling Above Function
alert(a); // 02/05/2016
var pastDate = today.setFullYear(today.getFullYear() - 1);
var b = convertDate(pastDate);
alert(b); // 02/05/2015
$("#date-picker input").datepicker({format: 'dd/mm/yyyy'}).datepicker("setDate", pastDate); //Set newDate in your DatePicker what ever you want .
</script>
以下是您可以使用的一些额外日期 - 时间相关功能(使用( - / +)根据过去和未来的时间需求)
**Method** **Description**
getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getFullYear() Returns the year (four digits)
getHours() Returns the hour (from 0-23)
getMilliseconds() Returns the milliseconds (from 0-999)
getMinutes() Returns the minutes (from 0-59)
getMonth() Returns the month (from 0-11)
getSeconds() Returns the seconds (from 0-59)
您还可以参考Mozila开发者网络Date - JavaScript _ MDN
和W3Schools JavaScript Dates了解更多信息
答案 2 :(得分:0)
您可以使用接受“日期”的setDate
,例如相对日期字符串,例如"-1y"
示例:
$("#testDateField").datepicker({
dateFormat: "dd/mm/yy"
}).datepicker("setDate", "-1y");
示例小提琴: