Skip to main content

Facebook Like Time Ago Function C#

This is a small time ago function which return a date time value in a readable way as facebook,twitter and most of the forum sites..eg(1 day ago,1 min ago etc).used c# programming language..

C#



public static string TimeAgo( DateTime date)
     {

     TimeSpan timeSince = DateTime.Now.Subtract(date);
     if (timeSince.TotalMilliseconds < 1)
        return "not yet";
     if (timeSince.TotalMinutes < 1)
        return "just now";
     if (timeSince.TotalMinutes < 2)
        return "1 minute ago";
     if (timeSince.TotalMinutes < 60)
        return string.Format("{0} minutes ago", timeSince.Minutes);
     if (timeSince.TotalMinutes < 120)
        return "1 hour ago";
     if (timeSince.TotalHours < 24)
        return string.Format("{0} hours ago", timeSince.Hours);
     if (timeSince.TotalDays == 1)
        return "yesterday";
     if (timeSince.TotalDays < 7)
        return string.Format("{0} days ago", timeSince.Days);
     if (timeSince.TotalDays < 14)
        return "last week";
     if (timeSince.TotalDays < 21)
        return "2 weeks ago";
     if (timeSince.TotalDays < 28)
        return "3 weeks ago";
     if (timeSince.TotalDays < 60)
        return "last month";
     if (timeSince.TotalDays < 365)
        return string.Format("{0} months ago", Math.Round(timeSince.TotalDays / 30));
     if (timeSince.TotalDays < 730)
        return "last year";

     //last but not least...
     return string.Format("{0} years ago", Math.Round(timeSince.TotalDays / 365));

     }

private void check()
    {
        DateTime date=Convert.ToDateTime("10/11/2012 4:15:47 PM");
     
   string   b=  TimeAgo( date);
        Response.Write(b);
    }

protected void Page_Load(object sender, EventArgs e)
    {
        check();

    }

Output:
you will get out as 1 day ago,1 week ago etc etc...

Popular posts from this blog

Dot Net FrameWork

The .NET Framework has two main components: the 1)Common Language Runtime (CLR) and the 2).NET Framework class library. The .NET Framework provides a Runtime environment called the Common Language Runtime or (CLR) that handles the execution of the code and provides useful services for the implementation of the application. CLR: The Runtime can be considered an agent that manages code at execution time. Thus providing core services such as memory management, thread management, and remoting. Also incorporating strict type safety, security and robustness. The Common Language Runtime (CLR) is the virtual machine component of the .NET framework. All .NET programs execute under the supervision of the CLR, guaranteeing certain properties and behaviors in the areas of memory management, security, and exception handling. Class Library: The class library is a comprehensive collection of reusable types that you can use to develop traditional command-line, WinForm (graphical user interface) appli...

Angry Bird

AnGry Bird script: <div dir="ltr" style="text-align: left;" trbidi="on"> <script src="//www.gmodules.com/ig/ifr?url=http://learningphp.freehosting.com/aeykay.xml&amp;synd=open&amp;w=500&amp;h=400&amp;title=Angry+Birds&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js"></script></div> Demo:

Change date format in jquery datepicker

Change date format in jquery datepicker We can change the date format in jquery ui datepicker. <script> $(function() { $("#datepicker1").datepicker({showOn: 'both', buttonImage: 'images/calendar.gif', buttonImageOnly: true, changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'dd-mm-yy', yearRange: '1900:2025' }); }); </script> Demo:Normal JQuery ui datepicker Date: Demo:Changed JQuery ui datepicker Date: