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

List Of Most Searched Topics On Google 2016

List Of Most Searched Topics On Google                                                                                                                               source Global Searches   1) Pokémon Go  2) iPhone 7   3) Donald Trump   4) Prince   5) Powerball   6) David Bowie   7) Deadpool   8) Olympics   9) Slither.io  1 0) Suicide Squad Global News   1) US Election  2) Olympics   3) Brexit   4) Orlando Shooting  5) Zika Virus   6) Panama Papers  7) Nice 8) Brussels  9) Dallas Shooting  10) 熊本 地震 ( Kumamoto Earthquake) People   1) Donald Trump   2) Hillary Clinton   3) Michael Phelps   4) Melania Trump   5) Simone Biles   6) Bernie Sanders   7) Steven Avery   8) Céline Dion   9) Ryan Lochte  10) Tom Hiddleston Consumer Tech   1) iPhone 7   2) Freedom 251   3) iPhone SE   4) iPhone 6S   5) Google Pixel   6) Samsung Galaxy S7   7) iPhone 7 Plus   8) Note 7   9) Nintendo Switch  10) Samsung J7 Global Sporting Events   1) Rio O

THE BEST BRAIN TRAINING APPS FOR ANDROID

THE BEST BRAIN TRAINING APPS FOR ANDROID Elevate - Brain Training Elevate is a brain training program designed to improve attention, speaking skills, processing speed, memory, math skills, and more. Each person is provided with his or her own personalized training program that adjusts over time to maximize results. The more you train with Elevate, the more you’ll improve critical cognitive skills that are designed to boost productivity, earning power, and self-confidence. Users who train at least 3 times per week have reported dramatic gains and increased confidence. FEATURES * 35+ brain games for critical cognitive skills like focus, memory, processing, math, precision, and comprehension * Detailed performance tracking * Personalized daily workouts that include the skills you need most * Adaptive difficulty progression to ensure your experience is challenging * Workout calendar to help you track your streaks and stay motivated Peak - Brain Training P

Introduction to Programming language C

Dennis Ritchie ( US) designed C programming language in 1972 at "AT & T's Bell laboratories". C has been often termed as " M iddle Leve l Language " . Programs written in C are very efficient and fast. C is a general purpose structured powerful language. Features at a Glance: • C is highly portable language .This means that a C program written for one system can be run on another system with little or no modifications. • C's another striking feature is its ability to extend itself. We can add our own functions to the C library . • Writing a C program with user defined makes program more simple and easy to understand . • C - language has more data types ( except boolean operators ), operators and can take more variables than other languages. • C - language is a structured programming language that is  it has different modules and blocks . • C - language is used to develop graphics software . 2D and 3D graphics are also implemented using C.