Click here to Skip to main content
13,356,318 members (61,568 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 30 Jan 2014

Code to Turn your TimeSpan into User Readable and Friendly Text

, 30 Jan 2014
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

You may also be interested in...


Comments and Discussions

QuestionNot enough! Pin
Afzaal Ahmad Zeeshan10-Nov-14 10:41
professionalAfzaal Ahmad Zeeshan10-Nov-14 10:41 
AnswerRe: Not enough! Pin
Tomer Weinberg15-Nov-14 8:13
memberTomer Weinberg15-Nov-14 8:13 
Questionwhere is Prettify extension method Pin
Tridip Bhattacharjee12-Mar-14 9:44
memberTridip Bhattacharjee12-Mar-14 9:44 
GeneralMy vote of 2 Pin
PSU Steve31-Jan-14 8:18
memberPSU Steve31-Jan-14 8:18 
GeneralMy vote of 1 Pin
phil.o31-Jan-14 5:53
memberphil.o31-Jan-14 5:53 
GeneralRe: My vote of 1 Pin
Tomer Weinberg15-Nov-14 8:15
memberTomer Weinberg15-Nov-14 8:15 
QuestionCode Link Pin
Hollerith31-Jan-14 3:48
memberHollerith31-Jan-14 3:48 
QuestionGood idea, but not so good reasons. Pin
JV999931-Jan-14 1:56
professionalJV999931-Jan-14 1:56 

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

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

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