How to show Friendly Date in SharePoint using Javascript or using Server side code

 

JAVASCRIPT

You can use following javascript code in order to show Date field value in Friendly format


var yourDate = new Date();

var friendlyDate = SP.DateTimeUtil.SPRelativeDateTime.getRelativeDateTimeString(yourDate , true, SP.DateTimeUtil.SPCalendarType.none, false)

console.log(friendlyDate);

 

Note that you need to add reference of “SP.dateTimeUtil.js” js in your page

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

Following is the description about method argument:
1. Your date (Date ISO8601)
2. Specify whether want to include tiem or not(IncludeTime)
3. Calendar type (CalendarType)
4. Specify whether want to adjust time with server(AdjustToServersNowTime)
————————————————————————–
SERVER SIDE


DateTime date = DateTime.Now;
string friendlyDate = SPRelativeDateTime.GetRelativeDateString(web, date.ToUniversalTime(), date); 

I Hope it will same someone’s time.
 

Advertisements

3 thoughts on “How to show Friendly Date in SharePoint using Javascript or using Server side code

  1. ‘IncludeTime’ flag does not affest in my Online.

    code:

    // need to add reference of ‘SP.dateTimeUtil.js’
    console.log(“ate:” + eventStart);
    var friendlyDate = SP.DateTimeUtil.SPRelativeDateTime.getRelativeDateTimeString(eventStart, true, SP.DateTimeUtil.SPCalendarType.none, false);
    console.log(“Friendly date: ” + friendlyDate);
    ———————————————————-
    result:
    Date:Thu Oct 05 2017 18:00:00 GMT+0300 (GTB Daylight Time)
    Friendly date: October 05

    Like

Leave a Reply to Rockie Cancel reply

Fill in your details below or click an icon to log in:

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