Skip to main content

Facebook / Twitter Like Time date Conversion in ASP.NET C# / MVC


Management of date time system in web application is an important task of a website programmer. Facebook and Twitter like website uses special conversion algorithms for time representation and synchronization. On the other hand Programming language like C#, stores date time in there special formats for system uses.  We can use .ToString(<FORMAT>) method for special time-date formatting. You can read more about this on Microsoft Website.

I have create a mehtod/function to obtain behavior of this date-time in c# programming language.
Function Accepts the Date-Time type Parameters and returns the string format of date time for user friendly representations.

Here is implementation in C# Language:

public string TimeFromDateTime(DateTime dt)
        {
           TimeSpan span = DateTime.Now - dt;
           if (dt < DateTime.Now)
           {
               if (span.Days > 365)
               {
                   return dt.ToString("dd MMMM yyyy");
               }
               if (span.Days > 30)
               {
                   return dt.ToString("ddd, dd MMMM yyyy");
                  
               }
               if (span.Days > 0)
               {
                   return dt.ToString("ddd, d MMM h:mm tt");
               }
               if (span.Hours > 0)
               {
                   return String.Format("{0} {1} ago", span.Hours, span.Hours == 1 ? "hr" : "hrs");
               }
               if (span.Minutes > 0)
               {
                   return String.Format("{0} {1} ago", span.Minutes, span.Minutes == 1 ? "min" : "mins");
               }
               if (span.Seconds > 5)
               {
                   return String.Format("{0} sec ago", span.Seconds);
               }
               if (span.Seconds <= 5)
               {
                   return "just now";
               }
           }
           else
           {
               span = dt - DateTime.Now;
               if (span.Days > 365)
               {
                   return "Upcoming " + dt.ToString("dd MMMM yyyy");
               }
               if (span.Days > 30)
               {
                   return "Upcoming "+dt.ToString("dd MMMM yyyy");
               }
               if (span.Days > 0)

                   return String.Format("about {0} {1} from now", span.Days, span.Days == 1 ? "day" : "days");
               if (span.Hours > 0)
                   return String.Format("about {0} {1} from now", span.Hours, span.Hours == 1 ? "hour" : "hours");
               if (span.Minutes > 0)
                   return String.Format("about {0} {1} from now", span.Minutes,
                                        span.Minutes == 1 ? "minute" : "minutes");
               if (span.Seconds > 5)
                   return String.Format("about {0} seconds from now", span.Seconds);
               if (span.Seconds == 0)
                   return "just now";
           }
            return "Unknown Time is Left";
         }

Thankyou For reading this post. Report any problem or error in comments section.
#WithLoveFromJaskaranSHSD

Comments

Popular posts from this blog

C-LIBRARIES

The C programming language uses libraries as its primary method of extension. In C , a library is a set of functions contained within a single "archive " file . Each library typically has a header life , which contains the prototypes of the functions contained within the library that may be used by a program , and declarations of special data types and macro symbols used with these functions .               The most common C library is the C standard library , which is specified by  the ISO and ANSI C standards and comes functions with every C implementation.     Another common set of C library functions are those used by applications specifically targeted for Unix like systems , especially functions which provide an interface to the kernel.       Libraries are often written in C because C compilers generate efficient object code ; programmers then create interfaces to the li...

C-Operators

An operator is a symbol that operates on a certain data types and produce the output as the result of the operation. Category of Operators • Unary Operator An unary operator is an operator which operates on one operand that is it operates on itself.            Operand1 operator Here Operand1 may be a variable , a constant or an expression etc.    For example: -b , ~a , x+1 etc. • Binary Operator A binary operator is an operator which operates on two operands. Operand1 operator Operand2 Here Operand1 and Operand2 maybe a variable, a constant or an expression etc. and Operator must be either arithmetic type or relational type or logical type etc.    For example: a+b , a && b , etc. • Ternary Operator A ternary operator is an operator which operates on 3 operands. Operand1 o perator Operand2 operator Operand3   Here Operand1 , Operand 2 , Operand3 may be a variable ...

Removing HTML Tags from string in C#

Hello friends, With this very first post of our new Blog GimiBits, we start our whole new journey contributing to information technology. Our First post is about the logic to remove the HTML tags from the given string with C# (C Sharp) programming language. Logic is very Simple to Implement, we take up a string and for its each index char we find the opening '<' braces and ignores the inside content until the closing '>' braces is not encountered and save to another char[] datatype. Here is a function based Implementation of the logic:  public string RemoveHtmlTags(string Html)         {             string content = Html;             char[] array = new char[content.Length];             int arrayIndex = 0;           ...