Click here to Skip to main content
Click here to Skip to main content

Code to Turn your TimeSpan into User Readable and Friendly Text

, 30 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A piece of code you can copy paste to transform TimeSpan into user readable UI text


Programmers and scientists like numbers, users don't, therefore, I take the steps to convert all numbers in my Web-UI to Colors, Sizes (Progress bars, etc.), Icons and such.
And the case of Date and Time is no exception!

The saying: "sent at 4/5/2012 15:43" has so many issues:

  1. It takes a while to figure out, this is 2 years ago...
  2. Is the hour really meaningful for a 2 year old post?
    Even the day might be neglectable.
  3. No one ever knows if the date is dd/mm/yyyy or mm/dd/yyyy


To provide a better UX, I think Facebook nailed it by stamping posts with readable times such as: few seconds ago, an hour ago, 2 years ago and such.

I built a similar transformation for C# TimeSpan class and wanted to share it with all.

It will provide a string with N parts for each valid part of the TimeSpan as 4 years 3 months is two parts.

Using the Code

Simply copy the class to your code-base and when needed, call the extension method Prettify() on any TimeSpan object.


DateTime dt = new DateTime(...); // any dateTime
string formatedDate = DateTime.Now.Substract(dt).Prettify(); 

You can control the output by two parameters:

  1. partCount: means how many NON-ZERO parts you want.
  2. neglectionFactor: Don't output a part if it doesn't affect the overall value by this much means if number of years is very high, months become insignificant and will not be displayed.

Note (Localization)

The code is made to work in Hebrew, but is easily translated to any other language and dialect.
Just change the Get*Text(int v) functions to return other values.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tomer Weinberg

United States United States
No Biography provided

Comments and Discussions

QuestionNot enough! PinprofessionalAfzaal Ahmad Zeeshan10-Nov-14 10:41 
AnswerRe: Not enough! PinmemberTomer Weinberg15-Nov-14 8:13 
Questionwhere is Prettify extension method PinmemberTridip Bhattacharjee12-Mar-14 9:44 
GeneralMy vote of 2 PinmemberPSU Steve31-Jan-14 8:18 
GeneralMy vote of 1 Pinmemberphil.o31-Jan-14 5:53 
GeneralRe: My vote of 1 PinmemberTomer Weinberg15-Nov-14 8:15 
QuestionCode Link PinmemberHollerith31-Jan-14 3:48 
QuestionGood idea, but not so good reasons. PinprofessionalJV999931-Jan-14 1:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 30 Jan 2014
Article Copyright 2014 by Tomer Weinberg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid