User Friendly Time in time ago format for SharePoint Web Parts and C#.NET

In this article i will tell you how to use normal date time in User friendly format programmatically in SharePoint Custom Web Parts and C#.NET.

This is a pretty cool look we can use for displaying time. The Output looks like :

User friendly time in SharePoint Programmatically

Here is the sample code to Create User Friendly Time in SharePoint Programmatically using C#.NET.

 /// <summary>
 /// Method used to Get the Datetime in User Friendly Date Format
 /// </summary>
 public static string GetUserFriendlyTime(DateTime dt, DateTime dtNowDate)
 {
  TimeSpan span = dtNowDate - dt;
  if (span.Days > 365)
  {
   int years = (span.Days / 365);

  if (span.Days % 365 != 0)
   years += 1;

   return String.Format("about {0} {1} ago", years, years == 1 ? "year" : "years");
   }

  if (span.Days > 30)
  {
   int months = (span.Days / 30);

  if (span.Days % 31 != 0)
    months += 1;

  return String.Format("about {0} {1} ago", months, months == 1 ? "month" : "months");
  }

  if (span.Days > 0)
  return String.Format("about {0} {1} ago", span.Days, span.Days == 1 ? "day" : "days");

  if (span.Hours > 0)
  return String.Format("about {0} {1} ago", span.Hours, span.Hours == 1 ? "hour" : "hours");

  if (span.Minutes > 0)
  return String.Format("about {0} {1} ago", span.Minutes, span.Minutes == 1 ? "minute" : "minutes");

  if (span.Seconds > 5)
  return String.Format("about {0} seconds ago", span.Seconds);

  if (span.Seconds <= 5)
  return "just now";

  return string.Empty;
 }

Hope this information is helpful for you.
User Friendly Time in time ago format for SharePoint Web Parts and C#.NET User Friendly Time in time ago format for SharePoint Web Parts and C#.NET Reviewed by Sudheer Gangumolu on December 18, 2014 Rating: 5

5 comments:

  1. Go into list configuration, select edit date column and deactivate friendly display format.

    ReplyDelete
  2. How do I apply this code to a content query web part on a custom page. I have not worked with C#.NET before. What do I need to edit to get this to work.

    ReplyDelete
    Replies
    1. Hi, This code is used in the Visual Web parts and C# code.

      Delete
  3. Hello Sudheer, I’m a software developer dubai based and I read your article it is quite impressive that how easily and simply your detailed the C#.Net coding for the creation of friendly time. I asked to tried to issue myself and ask many of my friend and then I got the answer for the question but you really simplified the it specially for the newbie in the coding market. Learning code is the demand of current era and also market need and people like you helping to promote and encourage youngsters towards coding by simplifying it.

    ReplyDelete
  4. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.

    Best Software Development Agency Dubai UAE

    ReplyDelete