Click here to Skip to main content
15,887,485 members
Articles / General Programming / String
Tip/Trick

Code to Turn your TimeSpan into User Readable and Friendly Text

Rate me:
Please Sign up or sign in to vote.
2.90/5 (5 votes)
30 Jan 2014CPOL1 min read 16.1K   40   4   8
A piece of code you can copy paste to transform TimeSpan into user readable UI text

Introduction

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

Background

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.

Example:

C++
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.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot enough! Pin
Afzaal Ahmad Zeeshan10-Nov-14 9:41
professionalAfzaal Ahmad Zeeshan10-Nov-14 9:41 
AnswerRe: Not enough! Pin
Tomer Weinberg15-Nov-14 7:13
Tomer Weinberg15-Nov-14 7:13 
Questionwhere is Prettify extension method Pin
Tridip Bhattacharjee12-Mar-14 8:44
professionalTridip Bhattacharjee12-Mar-14 8:44 
GeneralMy vote of 2 Pin
PSU Steve31-Jan-14 7:18
professionalPSU Steve31-Jan-14 7:18 
GeneralMy vote of 1 Pin
phil.o31-Jan-14 4:53
professionalphil.o31-Jan-14 4:53 
GeneralRe: My vote of 1 Pin
Tomer Weinberg15-Nov-14 7:15
Tomer Weinberg15-Nov-14 7:15 
QuestionCode Link Pin
Hollerith31-Jan-14 2:48
Hollerith31-Jan-14 2:48 
QuestionGood idea, but not so good reasons. Pin
JV999931-Jan-14 0:56
professionalJV999931-Jan-14 0:56 
I don't really agree on your first three points:
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

1 -> I have not met a single person yet (with an age on which they understand clocks, time and such) who has problems with figuring out that the date was 2 years ago. You sometimes make mistakes, but that happens with everything.
2 -> I agree, but what's the functional reason to show the time there? I think that's a more appropriate question in this case to determine whether it's correct to show it.
3 -> Everybody should know what the format is because A) You have a application standard or B) A company standard or C) The country standard for date formatting. If the formatting is al over the place this is a problem with how you company or application deals with localization and not a problem with how the date is shown.

So I don't agree with your reasoning. You are trying to solve different kind of problems by blaming it on date formatting.

However there is defiantly room for your code in my opinion, because as you state in some cases it's more interesting to just know how old the post is regardless of an absolute time. Your facebook example is perfect; You don't need (or want) to know when the post was exactly made. Instead you are more interested in how long ago the post was made.

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.