none
Show current month in newform.aspx base on system time

    Question

  • Hi

    In one of my sharepoint list, I have choice field that show months. I want to when user open newform.aspx this choice field show current month base on system time. How I can do it?

    Thanks.

    Sunday, May 20, 2012 3:17 AM

Answers

All replies

  • You could use some javascript on the page to get the Month value of the current date. then set the value of the choicce field = to that value.
    • Proposed as answer by Aviw_ Sunday, May 20, 2012 6:58 AM
    Sunday, May 20, 2012 4:51 AM
  • Can you help me what javascript code should I use? Can you explain more or give me some helpful link or exact code?

    thanks.

    Sunday, May 20, 2012 5:04 AM
  • Hello!

    Add Content Editor Web Part and add following code:

    <div>
      <script>
        var monthNames = "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];
        var month = (new Date()).getMonth();
        document.write("The current month is " + monthNames[month]);
      </script>
    </div>

    My contributions: SharePoint 2010 Solution Installer
    SharePoint How-To: Create WebPart Page programmatically

    Sunday, May 20, 2012 6:57 AM
  • Hi Aviw,

    In choice field I add January to December as a month, also add your JavaScript code in Content Editor WebPart,but when i add new item I did not see for example “The current month is May”.

    I want to when newform.aspx appears,base on system time load appropriate month in choice field automatically. 

    Monday, May 21, 2012 3:31 AM
  • Open the Form in Infopath and add Control to it.


    Ashish Mishra MCTS, MCPD in SharePoint 2010

    Monday, May 21, 2012 6:18 AM
  • Go to edit new form page:

    and add new Content Editor Web Part.

    Add the following script to it:

    $(document).ready(function(){
      var monthNames = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
      var month = (new Date()).getMonth();
      $("input[title='YourFieldTitle']").val(monthNames[month]);
    });

    Also ensure field title (in script) and link to jQuery library.

    My contributions: SharePoint 2010 Solution Installer
    SharePoint How-To: Create WebPart Page programmatically


    Monday, May 21, 2012 7:03 AM
  • Thanks Aviw,

    What’s your mean about link to jQuery library? I’m beginner to use java script code.can you explain more.

    Thanks a lot.

    Tuesday, May 22, 2012 3:21 AM
  • 1. Download jQuery library from here.

    2. Put it to this folder: "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\"

    3. Add link to library before your script: "<script type="text/javascript" src="/_layouts/jquery-1.7.2.min.js"></script>"

    Your result script will be something like this:

    <script type="text/javascript" src="/_layouts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      var monthNames = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
      var month = (new Date()).getMonth();
      $("input[title='YourFieldTitle']").val(monthNames[month]);
    });
    </script>
    Also look here about the ways how to integrate jQuery library to your site. (If your are going to use it in the sequel)

    My contributions: SharePoint 2010 Solution Installer

    Tuesday, May 22, 2012 7:11 AM
  • Aviw,

    I set this settings step by step, but it does not work!

    1-     In my choice field add month name: January, February, March, April, May, June, July, August, September, October, November, December. Without  Default Value.

    2-     Download jQuery library from your link.

    3-     Put it to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\" .

    4-     In NewForm.aspx page go to site settings>Edit Page and add Content Editor Web Part. And in Edit HTML Source, add below code:

    <script type="text/javascript" src="/_layouts/jquery-1.7.2.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

      var monthNames = "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

      var month = (new Date()).getMonth();

      $("input[title='Month']").val(monthNames[month]);

    });

    </script>

    Finally save this settings by click on  "stop editing" button. Now when I want to add new item , I don’t see current month in my choice field.

    What’s wrong?!


    • Edited by luckstar Sunday, June 17, 2012 4:01 PM
    Sunday, June 17, 2012 4:01 PM
  • Hello!

    $("input[title='Month']")

    Here Month is the title of your list column. Set your value.


    Also try to get access to jQuery file by going by url http://server/_layouts/jquery-1.7.2.min.js


    My contributions: SharePoint 2010 Solution Installer

    Sunday, June 17, 2012 4:47 PM
  • Aviw,

    In $("input[title='Month']") , ‘Month’ is my list column name.

    Also change code to address jQuery file by servername(http://itc-sppr) such this:

    <script src="http://itc-sppr/_layouts/jquery-1.7.2.min.js" type="text/javascript"></script>

    But it dosen’t work yet L

    Monday, June 18, 2012 6:19 AM
  • Try to debug this code in Internet Explorer Developer Panel (press F12 in IE and go to Script tab).

    My contributions: SharePoint 2010 Solution Installer

    Monday, June 18, 2012 6:59 AM