C#: Facebook like time stamp in

C#
Copy to clipboard
fablecode
  1. // Source: http://www.justinphillips.co/featured/facebook-like-time-stamp-in-c
  2.  
  3.  
  4. protected string Duration(String DateGiven)
  5. {
  6.     DateTime Date;
  7.     DateTime Now = DateTime.Now;
  8.     string Tense; // This will be either past or future
  9.  
  10.     // Check for valid date.
  11.     if(DateTime.TryParse(DateGiven, out Date))
  12.     {
  13.         // Houston we have a go!!!
  14.     }
  15.     else
  16.     {
  17.         return "Please provide a valid date";
  18.     }
  19.  
  20.     // Compare the date and time now to the one given.
  21.     double Comparison = (double)DateTime.Compare(Date, Now);
  22.  
  23.     // Is it future date or past date?
  24.     if(Comparison < 0)
  25.     {
  26.         Tense = "ago";
  27.     }
  28.     else
  29.     {
  30.         Tense = "from now";
  31.     }
  32.  
  33.     // Setup collections for determining the period
  34.     String[] Periods = new String[8] { "second", "minute", "hour", "day", "week", "month", "year", "decade" };
  35.     double[] Lengths = new double[7] { 60, 60, 24, 7, 4.35, 12, 10 };
  36.  
  37.     // Determine what the period between now and the given is.
  38.     double Diff = Now.Subtract(Date).TotalSeconds;
  39.     int j;
  40.     for(j = 0; Diff >= Lengths[j] && j < Lengths.Length; j++)
  41.     {
  42.         Diff /= Lengths[j];
  43.     }
  44.  
  45.     // round the double
  46.     Diff = Math.Round(Diff);
  47.  
  48.     // If in the past, remove negative.
  49.     if (Diff < 0)
  50.     {
  51.         Diff *= -1;
  52.     }
  53.  
  54.     // Add plural period if needed.
  55.     if(Diff != 1)
  56.     {
  57.         Periods[j] += "s";
  58.     }
  59.  
  60.     return Convert.ToString(Diff) + " " + Periods[j] + " " + Tense;
  61. }

    Leave a comment

     
    Visual verification
     

    Related books

    More InfoLearn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    Learn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    More InfoC# 6.0 and the .NET 4.6 Framework
    C# 6.0 and the .NET 4.6 Framework
    More InfoMicrosoft Visual C# Step by Step (8th Edition) (Developer Reference)
    Microsoft Visual C# Step by Step (8th Edition) (Developer Reference)
    More InfoThe C# Player's Guide (3rd Edition)
    The C# Player's Guide (3rd Edition)
    More InfoThe C# Player's Guide (2nd Edition)
    The C# Player's Guide (2nd Edition)
    More InfoC# 6.0 in a Nutshell: The Definitive Reference
    C# 6.0 in a Nutshell: The Definitive Reference
    More InfoC# Programming: The No-Nonsense Guide: Learn C# Programming Within 12 Hours! (Including A Free C# Cheatsheet & 30+ Exercises)
    C# Programming: The No-Nonsense Guide: Learn C# Programming Within 12 Hours! (Including A Free C# Cheatsheet & 30+ Exercises)
    More InfoC#: C# Crash Course - The Complete Beginner's Course to Learn C# Programming in 15 Clear-Cut Lessons - Including Dozens of Practical Examples & Exercises (C# Series)
    C#: C# Crash Course - The Complete Beginner's Course to Learn C# Programming in 15 Clear-Cut Lessons - Including Dozens of Practical Examples & Exercises (C# Series)