3

I am updating a list and it's views (put together by another developer) and am adding a new date column.

Currently, there is/was an existing date column (in this case called Orig Due Date), and I added the new column, Revised Due Date.

enter image description here

Question I have is, Orig Due Date is formatting the date in a contextual way (2 days ago, Yesterday, Thursday, etc) and I cant seem to figure out how this is being done. There doesnt seem to be anything different in the column settings compared to my new Revised Due Date column, and I cant seem to figure out if it's some other JavaScript that is maybe doing it.

Any idea how I can apply the same formatting to my Revised Due Date column, or else remove the contextual formatting of Orig. Due Date?

Thanks!

5

If I understood you correct you want to set your date as yesterday, two days ago, etc. and not just to see actual date 18.08.2015. If so, there is choice field when creating date column, name of that field is Display Format. you should choose Friendly option, default option is Standard.

I do apologize if you meant something different to ask.

  • 1
    nice one! Cant believe I missed that option.. thanks! Did exactly what I wanted, and is possibly the most effective solution. The only 'catch' with this is that it doesnt format with the year. So after '6 days ago' it becomes 'August 12'. We want to provide a year context as well, so 'August 12, 2015'. I fear this might ultimately require custom javascript manipulation working with SPRelativeDateTime. But thanks for your answer! – Simon Aug 19 '15 at 8:00
  • Glad I could help. Cheers. – Danilo Aug 19 '15 at 8:10
5

In case you use JavaScript:

First of all you need a reference to SP.dateTimeUtil.js:

<script type="text/javascript" src="/_layouts/15/SP.dateTimeUtil.js"></script>

This file contains a JavaScript function called getRelativeDateTimeString() accepting four parameters:

  • Date ISO8601
  • IncludeTime
  • CalendarType
  • AdjustToServersNowTime

You call the function like this:

SP.DateTimeUtil.SPRelativeDateTime.getRelativeDateTimeString(new Date(), true, SP.DateTimeUtil.SPCalendarType.none, false);

This should return a string like "A few seconds ago". Credits go to this blog post (German).

In case you use C# (Server Objet Model):

Microsoft.SharePoint.Utilities.SPRelativeDateTime seems to provide some static methods with similar functionality.

  • Thanks JCP. So Im hoping to understand how to use this... in my instance, I have gotten the string date of my first Revised Due Date column and converted it to a JS date, ie var newDate = Date("2015-08-12"). I then called SP.DateTimeUtil.SPRelativeDateTime.getRelativeDateTimeString(newDate, true, SP.DateTimeUtil.SPCalendarType.none, false) but it then throws an error about Uncaught TypeError: a.getFullYear is not a function. Any idea why? Am I not using it correctly? – Simon Aug 19 '15 at 7:36
  • You've missed a new: var newDate = new Date("2015-08-12"); – jcp Aug 19 '15 at 7:45
  • ugh, im such an idiot. you're right, thanks! :) its working perfectly, thanks!! – Simon Aug 19 '15 at 7:47
  • As Danilo pointed out there is actually a setting I missed on the Date column field to use 'Friendly' date. While this answers the question more specifically, it seems I will still use your solution to fill in the missing year context to the date field, as the Friendly setting only returns August 12 and not August 12, 2015, which is preferred. So I will need to adjust the use of getRelativeDateTimeString and append the year at the end to get the desired result. Thanks again! – Simon Aug 19 '15 at 8:02
  • Oops. I thought your screenshot came from somthing custom made. But I didn't know that there is an OOTB way to get relative DateTime in "standard views". Lesson learned and +1 for Danilo :-) – jcp Aug 19 '15 at 8:08

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.