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