[英]How i can set date picker range with in a financial year based on the document date? Im using JQUERY to do this
I want to set my date picker range with in a financial year based on the document date.I get the document date , but i want to work this when the document is in edit mode and only for the particular Date section only.我想根据文档日期在财政年度中设置我的日期选择器范围。我得到了文档日期,但是我想在文档处于编辑模式并且仅适用于特定日期部分时进行此操作。
<div class="col-md-6 no-padding">
<div class="col-md-12">
@Html.LabelFor(model => model.EnquiryDateFormatted, htmlAttributes: new { @class = "control-label lblrequired" })
</div>
<div class="col-md-12">
@Html.EditorFor(model => model.EnquiryDateFormatted, new { htmlAttributes = new { @class = "form-control newinput datepicker", @autocomplete = "off",@id="enquiryDateID" } })
@Html.ValidationMessageFor(model => model.EnquiryDateFormatted, "", new { @class = "text-danger" })
</div>
</div>
You could use the the methods setStartDate and setEndDate for this. 您可以为此使用setStartDate和setEndDate方法。
The code will be something like this 该代码将是这样的
$('#yourDatePickerID').datepicker('setStartDate', '04/01/2018');
$('#yourDatePickerID').datepicker('setEndDate', '03/30/2019');
or 要么
you could use data attributes data-date-start-date
and data-date-end-date
您可以使用数据属性
data-date-start-date
和data-date-end-date
<input type="text" class="form-control" data-date-end-date=“mm/dd/yyyy”>
Use this to get financial year 用它来获取财政年度
function getCurrentFinancialYear(strDocDate) {
var startYear = "";
var endYear = "";
var docDate = new Date(strDocDate);
if ((docDate.getMonth() + 1) <= 3) {
startYear = docDate.getFullYear() - 1;
endYear = docDate.getFullYear();
} else {
startYear = docDate.getFullYear();
endYear = docDate.getFullYear() + 1;
}
return {startDate : "01-Apr-" + startYear, endDate: "31-Mar-" + endYear };
}
alert(getCurrentFinancialYear("4/1/2018").startDate);
So your code should be like 所以你的代码应该像
$('#yourDatePickerID').datepicker('setStartDate', getCurrentFinancialYear(enquiryDateID).startDate );
$('#yourDatePickerID').datepicker('setEndDate', getCurrentFinancialYear(enquiryDateID).endDate);
You can use startDate
and endDate
setting to enforce start and end dates, but you need to create a function to get fiscal year range first: 您可以使用
startDate
和endDate
设置来实施开始日期和结束日期,但是您需要创建一个函数来首先获取会计年度范围:
function getCurrentFiscalYear(date) {
var dates = {};
var docDate = new Date(date);
var month = docDate.getMonth();
if (month > 3) {
dates.sDate = new Date(docDate.getFullYear(), 3, 1);
dates.eDate = new Date(dates.sDate.getFullYear() + 1, dates.sDate.getMonth() - 1, 31);
}
else {
dates.sDate = new Date(docDate.getFullYear() - 1, 3, 1);
dates.eDate = new Date(docDate.getFullYear(), dates.sDate.getMonth() - 1, 31);
}
return dates;
}
Then, assign the function above to datepicker instance through startDate
and endDate
properties (they also accept JS Date
instance besides of date string): 然后,通过
startDate
和endDate
属性将上述函数分配给datepicker实例(除了日期字符串外,它们还接受JS Date
实例):
$('input.datepicker').datepicker({
format: "dd-M-yyyy",
startDate: getCurrentFiscalYear($('input.datepicker').val()).sDate, // start date
endDate: getCurrentFiscalYear($('input.datepicker').val()).eDate, // end date
maxViewMode: 0,
todayBtn: "linked",
clearBtn: true,
autoclose: true,
todayHighlight: true
});
Note: 注意:
If you have multiple datepickers to handle calendar selection, change the selector in both startDate
and endDate
based from nearest element (eg using closest()
, siblings()
, find()
, etc.) 如果您有多个日期选择器来处理日历选择,请从最近的元素开始更改
startDate
和endDate
的选择器(例如,使用closest()
, siblings()
, find()
等)。
The example implementation can be seen in this fiddle . 在这个小提琴中可以看到示例实现。
Side note: 边注:
The EditorFor
containing htmlAttributes
parameter only works for MVC 5.1 and above. 所述
EditorFor
含有htmlAttributes
参数仅适用于MVC 5.1及以上。 If your MVC version is 5.0 or below, use TextBoxFor
helper instead: 如果您的MVC版本是5.0或更低版本,请改用
TextBoxFor
helper:
@Html.TextBoxFor(model => model.EnquiryDateFormatted, "{0:dd-MMM-yyyy}", new { @class = "form-control newinput datepicker", @autocomplete = "off", @id="enquiryDateID" })
here is the correct answer to my question @kiranvj give me the key to the answer with a small change 这是我的问题的正确答案@kiranvj给我小的答案
here i am posting his part code and my part code 我在这里张贴他的零件代码和我的零件代码
code from @kiranvj @kiranvj的代码
function getCurrentFinancialYear(strDocDate) {
var startYear = "";
var endYear = "";
var docDate = new Date(strDocDate);
if ((docDate.getMonth() + 1) <= 3) {
startYear = docDate.getFullYear() - 1;
endYear = docDate.getFullYear();
} else {
startYear = docDate.getFullYear();
endYear = docDate.getFullYear() + 1;
}
return {startDate : "01-Apr-" + startYear, endDate: "31-Mar-" + endYear };
}
and my part code 和我的零件代码
$('input.datepicker').datepicker({
format: "dd-M-yyyy",
maxViewMode: 0,
todayBtn: "linked",
clearBtn: true,
autoclose: true,
todayHighlight: true,
startDate: getCurrentFinancialYear(enqDate).startDate,
endDate: getCurrentFinancialYear(enqDate).endDate
});
function getCurrentFinancialYear(strDocDate) {
var startYear = "";
var endYear = "";
var docDate = new Date(strDocDate);
if ((docDate.getMonth() + 1) <= 3) {
startYear = docDate.getFullYear() - 1;
endYear = docDate.getFullYear();
} else {
startYear = docDate.getFullYear();
endYear = docDate.getFullYear() + 1;
}
console.log({startDate : startYear+"-03-01", endDate: endYear+"-03-31" });
return {startDate : startYear+"-03-01", endDate: endYear+"-03-31" };
}
$(document).ready(function () {
$('.datepicker').datepicker({
format: 'yyyy-mm-dd',
maxViewMode: 0,
todayBtn: "linked",
clearBtn: true,
autoclose: true,
todayHighlight: true,
startDate: getCurrentFinancialYear($('input.datepicker').val()).startDate,
endDate: getCurrentFinancialYear($('input.datepicker').val()).endDate
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.