04:01 -
No comments
Restrict to Select the date or year || MS CRM
Select particular days in date picker field in dynamics CRM:
Objective:
To select the particular days in date filed in dynamics CRM,
for example he can able to select in up to 1 year from today [21-4-16 to
21-4-17]. If he try to select past or above one year he will get the error
message and he can’t able to save the record.
Create date field in and give option as date only.
After that
Data the below JavaScript code. Call the event in onchange
function.
function mydatecalculation()
{
var now = new Date();
var samplecheck = Xrm.Page.getAttribute("new_datesample").getValue();
var oneDay = 1000 * 60 * 60 * 24;
if ( samplecheck == null || samplecheck== '')
{
Xrm.Page.getControl("new_datesample").clearNotification();
}
else
{
var diffYear = samplecheck.getFullYear() - now.getFullYear();
debugger;
if(diffYear > 1)
{
Xrm.Page.getControl('new_datesample').setNotification("Invalid Date");
}
else if(diffYear < 0)
{
Xrm.Page.getControl("new_datesample").setNotification("Invalid Date");
}
else
{
var startselect = new Date(samplecheck.getFullYear(), 0, 0);
var diffselect = samplecheck - startselect;
var selectday = Math.floor(diffselect / oneDay);
var startcurrent = new Date(now.getFullYear(), 0, 0);
var diffcurrent = now - startcurrent;
var currentday = Math.floor(diffcurrent / oneDay);
if(selectday >= currentday)
{
debugger;
var getyear = samplecheck.getFullYear();
var checkyear = getyear % 4;
var firstDate = new Date();
var secondDate = new Date(samplecheck);
var diffDays = null;
var checkdayes = null;
if(checkyear == 0)
{
diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
checkdayes = diffDays / 364;
if(checkdayes < 1)
{
Xrm.Page.getControl("new_datesample").clearNotification();
}
else
{
Xrm.Page.getControl('new_datesample').setNotification("Invalid Date");
}
}
else
{
diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
checkdayes = diffDays / 365;
if(checkdayes < 1)
{
Xrm.Page.getControl("new_datesample").clearNotification();
}
else
{
Xrm.Page.getControl('new_datesample').setNotification("Invalid Date");
}
}
}
}
}
}