1

How to bind the DateTimeField field with today's date using SPServices and jQuery? I can't use the default Today to set the column in the Sharepoint UI to use Today's Date as the default value in my SharePoint 2010 SPList. My date field control name is: Request Received Date

When the newform.aspx is loaded, I need to bind this field with today's date.

  <script type="text/javascript">
 _spBodyOnLoadFunctionNames.push("setRequestEndTime");
  // Set the Request End Time to today's date  
 function setRequestEndTime() 
{   
var d = new Date();
 d = (d.getMonth() + 1).toString()
  + "/" + d.getDate().toString()
  + "/" + d.getFullYear().toString()
 + " " + d.getHours().toString()
 + ":" + d.getMinutes().toString()
    + ":" + d.getSeconds().toString();
 //alert(d);
    today = new Date();   
    todayDay = today.getDate();   
    todayMon = today.getMonth() + 1;   
    todayYear = today.getYear();   
    //alert('setRequestEndTime');   
    var tags = document.getElementsByTagName('input');   
    for (var i=0; i < tags.length; i++) 
    {   
        //alert(' tags[' + i + '].id=' + tags[i].id);   
        if(tags[i].id.indexOf('ff2_new') > 0) 
        {   
          //alert('HIT tags[' + i + '].id=' + tags[i].id);   
            tags[i].value = todayMon + "/" + todayDay + "/" +  
         todayYear;   
        }   
    }   
  }   </script>

I tried to add the above code refer: (http://sympmarc.com/2009/05/20/pre-filling-column-values-in-a-sharepoint-form/") in a content editor web part. but I didn't get the value assigned to the datetime field. Sorry for my JavaScript skills, am unable to get what's meant by ff2_new.

Update:

  <script type="text/javascript">
  var $j = jQuery.noConflict(); 
 function PreSaveAction() 
 { 
   alert("here..........");
  var txtDueDate = $j(":input[title='Due By']").val(); 
 var txtReqstRecvdDate = $j(":input[title='RequestReceivedDate']").val();
 }

here the requestrecvdate is my today's date. if i set the todays date value, i am getting "undefined" here. so i am unable to compare the txtduedate with the requestrecvdate. Hence I am trying to bind the today's date with the requestrecvdate field instead of setting the column-settings--> Today's Date

bumped to the homepage by Community 7 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • Are trying to also bind the time, or just the date? – Erin L Sep 30 '15 at 14:04
  • I need just the Date only.Doesn't need the time part. I am stuck with multiple approaches with none of them working.! – SaMolPP Sep 30 '15 at 14:05
0

You can set the date to default to Today in the column settings. Select Date and Time for column type and select "Today's Date" under default value.

  • I am having some issues with this settings.When i tried to read this value after column settings--> todays date under default value, am unable to compare this value with the other datetime field.i am checking using javascript if start date > end date I need to populate a error message.this start date is my today's date. Added the updated code for this validation which is in a different CEWP which is fired when presaveaction is triggered. – SaMolPP Sep 30 '15 at 15:00
  • Can you use the list's validation settings instead of writing your own javascript? List Settings >> Validation settings, then enter your formula. I can't write the formula because I'm a little confused on your field names and rules. Do you have 3 date fields? Request Received Date, Start Date, and End Date? Which one is supposed to be greater than (or is it greater or equal to) today? Is there a reason you created a Request Received Date field instead of just using the Created date which is set automatically? – Erin L Sep 30 '15 at 15:57
0

You can add a content editor web part to your newform.aspx and then add the following code to you CEWP:

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function ($) {
    var d = new Date();
    d = (d.getMonth() + 1).toString()
    + "/" + d.getDate().toString()
    + "/" + d.getFullYear().toString()
    + " " + d.getHours().toString()
    + ":" + d.getMinutes().toString()
    + ":" + d.getSeconds().toString();

    var datetimecontrol = $("input[id^='Request_x0020_Received_x0020_Date']")[0];
    $(datetimecontrol).val(d);
});

</script>

You can also check out this article that explains several scenarios for the datetime picker control

  • Thanks for your reply. Can you pls explain "how to supply it to the page" – SaMolPP Sep 30 '15 at 12:05
  • 1
    @SaMolPPp Open the list in SharePoint designer in the "lists and libraries" section. In the list designer view you will see a list of forms associated with the list, there will generally be a "NewForm.aspx" form listed. Right click this and then select "Edit File in Advanced Mode" to expose the ASP .Net / HTML markup used for the form. Now, you can add you javascript code to NewForm.aspx – S.Dav Sep 30 '15 at 13:06
  • unfortunately,I dont have SP D installed on my dev.environment. i am having a site collection with this default splist created and i was told to build UI and perform some manipulations. – SaMolPP Sep 30 '15 at 13:11
  • Can you pls explain what exactly Marc meant by this line if(tags[i].id.indexOf('ff2_new') > 0) ? I went to the developers tool bar and i need to find the Date control and bind it. – SaMolPP Sep 30 '15 at 13:13
  • is there any inbuilt API in SPServices and jQuery to bind this datetime field in the SPList, when the pageload of newform.aspx and editform.aspx ? Am pretty new using this SPServices & jQuery,JavaScript model.I always used to go for visual web part and write SSOM code. – SaMolPP Sep 30 '15 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.