Set Default value to SharePoint DateTime Control

set default value to datetimepicker sharepoint 2013

Requirement:
User want to see the default values while pulling out the loan details report with the given start and end date. Where the start date should be today’s date and end date should be “12/31/8888” in a server side WebPart control. When user click on Submit button we should ensure that End date is not less than Start Date.

set default value to datetimepicker
set default value to datetimepicker
set default value to datetimepicker sharepoint 2013
set default value to datetimepicker sharepoint 2013

Read some SharePoint 2013 tutorials:

Solution:
We can implement this in both CSOM and c#.net. Since the requirement is to implement in a server side control. Below is the code snippet to achieve this:



SharePoint Date Control:

<SharePoint:DateTimeControl ID=”cldStartDt” runat=”server” />
<SharePoint:DateTimeControl ID=”cldEndDt” runat=”server” />
<asp:Label ID=”lblMessage” runat=”server” ></asp:Label>

C# code to populate default values:

cldStartDt.SelectedDate = Convert.ToDateTime(DateTime.Today.ToString());
cldEndDt.SelectedDate = Convert.ToDateTime(DateTime.MaxValue.AddYears(-1111).ToString());

“Convert.ToDateTime(DateTime.Today.ToString())” with this we can get today’s date in Date and Time format.
Convert.ToDateTime(DateTime.MaxValue.AddYears(-1111).ToString()) with this we get the end date as “12/31/8888”

Validation to ensure that EndDate is not less than Start Date:

Which the below code snippet we can achieve the validation.
int dtDiff = Convert.ToInt32(endDate.Date.Subtract(Convert.ToDateTime(cldStartDt)).Days);
if(dtDiff>0)
{
lblMessage.Text = “End Date must be greater than Start Date!!”;
}

set-default-value-to-datetimepicker-sharepoint-2013set default value to datetimepicker sharepoint online
set-default-value-to-datetimepicker-sharepoint-2013set default value to datetimepicker sharepoint online

Hope this helps.

Similar SharePoint 2013 Tutorials


Leave a Reply