This jQuery tutorial explains how to bind the current date or today’s date to a date picker using jQuery in SharePoint Online Office 365.
Here in this example, I was creating an HTML input form for SharePoint Online site. For that, I was using a date picker control. And my requirement was to bind current date to the date picker using jQuery.
The same jQuery code we can use to add a date picker control and set the default as of today’s date in SharePoint 2013/2016. The code you can add inside a script editor or content editor web part in SharePoint.
You can also learn how to create an HTML form in classic SharePoint sites, in modern SharePoint Online sites, you can not add any script editor web part.
HTML DatePicker jQuery
First, create a “DatePicker” by using the below HTML code:
<input type="date" id="SubmitDate" title="Submitdate"/>
Here in this HTML Code, I have given a date ID attribute as “SubmitDate”.
You can also use the jQuery date picker in SharePoint.
Set Current Date in DatePicker using jQuery
Now we have to bind this date picker to the current date or today’s date using JQuery. The JQuery code is given below-
var now = new Date(); var day = ("0" + now.getDate()).slice(-2); var month = ("0" + (now.getMonth() + 1)).slice(-2); var today = now.getFullYear() + "-" + (month) + "-" + (day); $('#SubmitDate').val(today);
Here in this JQuery Code, I have taken a Variable name as “now” and assigned current date to the date picker. That “now” variable, I have passed in the variable “day”, “month” and “today” using Query String as “now.getDate()”, “now.getMonth()” and “now.getFullYear()”. So that we can get the date as a UTC Format like (YY-MM-DD).
To set Today’s date, write like below.
The full code to bind current date or today’s date to a date picker using jQuery in SharePoint looks like below:
Once you run the code, you can see the output looks like below in the SharePoint web part page.
This SharePoint tutorial, we discussed how to bind the current date or today’s date using JQuery in SharePoint Online or SharePoint 2016/2013.
I am Bijay a Microsoft MVP (8 times – My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com