SharePoint 2020

The Vision for a Future of Clarity

Display Todays Date in a Calculated Column with Javascript

Here’s what I used to display Today date in a Calculated Column with nothing but JavaScript in SharePoint 2010.

After a lot of research and trial and error, I finally have it displaying:

To display a date like this: 17/5/2015

Use this code:

=CONCATENATE("<p id='mydate'><script>var currentDate = new Date();var day = currentDate.getDate();var month = currentDate.getMonth()+1;var year = currentDate.getFullYear();document.getElementById('mydate').innerHTML = ( day + '/' + month + '/' + year );</script></p>")

 

calcColumnTodaysDate

 

Don’t forget to use data type returned as Number(1, 1.0, 100) This will convert the code to HTML

For U.S. Date, just swap the day and month over like this:

( day + ‘/’ + month + ‘/’ + year )

( month + ‘/’ + day + ‘/’ + year )

 

Update: The above code will only work for the first entry as the id name, i.e. ‘mydate’, needs to be unique.

So all you need to do is use another column that has Unique values, like a counter, or you could even combine two columns as thus:

 =CONCATENATE("<p id='"&PosNum&""&[First Name]&"'><script>var currentDate = new Date();var day = currentDate.getDate();var month = ('0' + (currentDate.getMonth()+1)).slice(-2);var year = currentDate.getFullYear();document.getElementById('"&PosNum&""&[First Name]&"').innerHTML = ( day + '/' + month + '/' + year );</script></p>"

The above code works and displays todays date for every Item.

 Date will now show as 20/05/2015

Hope this saves you some time 😉

Advertisements

May 17, 2015 - Posted by | SharePoint 2010, SharePoint Javascript | ,

4 Comments »

  1. I can’t get this code to work for me. When i entered in the first code, it worked and you were right – it only updated the first item in my list. I tried to use your second code but I keep getting any error. In the two spots where you have [First Name] i have replaced it with a column name in my list. I haven’t touched anything else. When i try to save, i get a message stating “The formula refers to a column that does not exist.”. Am i missing something?

    Comment by bshsi | December 3, 2015 | Reply

    • Yes, the id refers to two columns “&PosNum&””&[First Name]&”. So you’ll need to replace both names. This was to allow a unique ID for each item displaying todays date.

      Also I tried using this method referenced in another calculated column but it wasn’t able to work.
      It seems to be for display purposes only.

      Thanks for trying it out.

      Comment by Brett | December 3, 2015 | Reply

      • What was the function of the second column you are referring to – the one indicated above as “&PosNum&”?

        Comment by Nevermind Nonesbusiness | February 15, 2017

      • This is to make the column unique otherwise it just displays the date in the first row.
        From my testing, I couldn’t get the date to do anything, it just displays today’s date.
        But thought I would post it in case somebody does find it useful.

        Comment by Brett | February 15, 2017


Post a Comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: