The method you didn’t know you needed

by Mads Kristensen 16. March 2008 05:02

Warning: The following post has the same effect as 19 cups of coffee before bedtime. If you are in doubt you will be able to handle it, I suggest you don’t read any further.

Every once in a while, you are presented with a product or a design or even an idea that you immediately think is da bomb even though you can’t really explain why. It may not be all that brilliant, but there is something lovable about it you can’t ignore or easily forget. It stays with you in your dreams until you go purchase the product or implement in your own. It’s what showbiz calls the x-factor.

What I have for you today, dear reader is nothing less than that. A method you need for all your projects, but have never realized it before. Trust me when I say that you might as well implement it everywhere right now or you are going to lose a lot of sleep until you do. Hold on to your seat and get ready to be amazed.

Let’s start by seeing what it is you do wrong today when you code your software and then see how to make it much better with a little brilliance.

The problem

When writing an application for either web or desktop, we often display dates and time to the visitor or user. It could be the date and time of an invoice or the time of arrival somewhere. What we have done in the past is simply writing out the time in a pretty boring way like so:

11:48:09 PM
or
23:48:09

You might be wondering what is wrong with the given examples and I understand that, because that is the way time has always been formatted in most parts of the world. In other parts the delimiter might be a dot or comma, but otherwise pretty much the same.

Now ask yourself if it is important for your users to know the precise time down to the minute and do they care about the seconds? The answer in most cases is a laud and clear NO. Nobody cares. We’ll ignore the cases where users do care because that would give us a laud and clear NO in 100% of the cases.

The solution

We just established that 100% of your users do not care about precise stated time. Instead let’s give them something that makes sense. Enter the brilliant method you didn’t know you needed.

The method is called ConvertToRelativeTime and it takes a DateTime as parameter and returns a string. If we stick to the above examples of the time 11:48:09 PM and feed it as a parameter to the brilliant method, then we get this output:

A quarter to midnight

Other outputs could be:

Half past eleven pm
About three am
A quarter past eight am 

I’ll give you a few seconds to wipe off the hot coffee you just spilled in your lap…

In a busy world where time is everything and money is made by the second, we need to relax when we can. This is one such way. A sort of an anti-stress approach to time display.

The idea about relative time is not mine. I got it from 37signals who got it from someone else. I fell in love with it the moment I saw it, and for the past week I’ve been dreaming and losing sleep because of it – just like you will. You don’t need to worry though, because I have written the code for you to grab and implement.

The code

Ok, it's actually two methods - a public and a private helper to the public. 

public static string ConvertToRelativeTime(DateTime date)

{

  string relative = string.Empty;

  int hour = date.Hour;

  string meridian = date.Hour > 12 ? "pm" : "am";

 

  if (date.Minute < 8)

  {

    relative = "about";

  }

  else if (date.Minute > 52)

  {

    relative = "about";

    hour++;

  }

  else if (date.Minute < 22)

  {

    relative = "a quarter past";

  }

  else if (date.Minute < 38)

  {

    relative = "half past";

  }

  else if (date.Minute <= 52)

  {

    relative = "a quarter to";

    if (hour == 23)

    {

      hour = 0;

      meridian = string.Empty;

    }

    else

    {

      hour++;

    }

  }

 

  if (hour == 0)

  {

    meridian = string.Empty;

  }

 

  string name = GetHourText(hour);

  return relative + " " + name + " " +  meridian;

}

 

private static string GetHourText(int hour)

{

  if (hour > 12)

    hour = hour - 12;

 

  switch (hour)

  {

    case 1: return "one";

    case 2: return "two";

    case 3: return "three";

    case 4: return "four";

    case 5: return "five";

    case 6: return "six";

    case 7: return "seven";

    case 8: return "eight";

    case 9: return "nine";

    case 10: return "ten";

    case 11: return "eleven";

 

    default:

      if (hour == 0)

        return "midnight";

      else

        return "twelve";

  }

}

If you tweak it a bit you can display time in even more relative terms. What about these examples:

Two and a half hour to lunch
About an hour past bedtime
Work day ends in about 3 hours

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

Tips and tricks

Comments

3/16/2008 5:55:37 AM #

Domenic

Underwhelmed. For one, 12-hour time? Ewww.

Hey, glad to see that BlogEngine finally has a sensible Live Preview model. When I return to blogging it'll be nice to have that there...

Then again, I just noticed that this underwhelming method is in the Live Preview. This _must_ be optional, not only because it's icky 12-hour time, but it's also several hours off (from my time zone---it should adjust based on user time zone, I think), and it's often useful to see what the time delay was between successive posts.

Oh gods, it's at the top of the post too! Eek.

Domenic United States |

3/16/2008 6:17:41 AM #

Mike van Zandwijk

Funny concept and thanks for sharing, Mads.

For news items or even blog posts, I'd rather see age instead of time, like:
"posted 2 days ago" and "commented 3 minutes ago".

As for time, we still struggle with different time zones and how calculate time offsets between blog author and web server. If you add the most important person in the mix, your reader, time stamping gets even harder.

You wrote about time zones earlier and with tongue-in-cheek I mentioned Swatch' universal Internet time.

Remember those @924 or @118 notations for example?

Pretty hard for human understanding (guess big reason it failed), but perhaps a potential x-factor for webserver-to-reader time conversion?

Mike van Zandwijk Netherlands |

3/16/2008 6:28:10 AM #

Nikolaj Winnes

Hm, why not an extension method - making the syntax soo nice:

time.ExpressedInRelativeTerms();

just my $0.02

Nikolaj Winnes Denmark |

3/16/2008 6:36:18 AM #

Mads Kristensen

@Domenic,
It's not being implemented in BlogEngine. I just added it to my theme cause it actually do rocks. You'll come around after spending the good part of next week sleepless Smile
The new comment preview will be in the next version though. You are right in one thing, and that is the the 12 hour time, but at most times you say the time as if there only are 12 hours. It's not that often people say It's a quarter past twenty-two, but that could be made optional.

@Mike,
Swatch Universal Internet Time is probably too geeky for most people. I for one wouldn't have a clue. Not a bad idea though.

@Nikolaj,
Of course, why didn't I think of that. You just earned $0.02 my friend.

Mads Kristensen Denmark |

3/16/2008 6:51:33 AM #

Dave Scriven

Now if the signature is changed to:

public static string ConvertToRelativeTime(this DateTime date)

It's even better!

Dave Scriven Australia |

3/16/2008 6:53:29 AM #

Dan Atkinson

I think people would be more concerned about how long ago it was. So for example, if a user did something at 03:48 then, if it's 09:24, the system would return 'nearly 6 hours ago'.

I think that would be more fun!

Dan Atkinson United Kingdom |

3/16/2008 6:55:25 AM #

Mads Kristensen

@Dan,
That would be more fun at certain scenarios like a blog comment. I should probably write such a method as well. Good pointer.

Mads Kristensen Denmark |

3/16/2008 4:23:36 PM #

Frank

I'm not a fan Frown. For one, we tend to do this naturally. We see 11:59:10 PM and think "oh, it's about midnight". For now, our brains are better computers than those available at stores... no need for this method at all.

As for a reason *not* to use it... it doesn't look like the time when you're skimming something. When quickly reading, "text text text text" looks like text; ##:##:## looks like time. I may not care about all the ## but without much brain power I can tell you it's a timestamp. My bank doesn't tell me I have "eleven hundred fifty three dollars and nineteen cents" in my bank account (partly because it's not true, I don't have that much =D) but rather I have $1153.19. Much, much easier to read.

That's my "two cents" ($0.02). =D

Frank United States |

3/16/2008 6:01:48 PM #

Mike van Zandwijk

Mads,

I'm sorry for being unclear (and for lacking better feedback on your original idea). Next attempt:

1. Your code really rocks: I'm amazed how fast you turned it from concept to C# (and use it live!)
2. Well written article: you drove your point after setting (pretty high) expectations in your opening
3. You're spot on: most want a meaningful time context indeed, I sure do too

So what's an ideal context and notation?

Your examples with "About/almost" is a great start. It's about removing noise (exact minutes and seconds). Most people find numbers easier to the human eye instead of reading 'half past ten', so what do you think of "around 9:30 in the morning" or "almost 10:00 in the evening"? (since not everyone understands military time, else it's just "almost 22:00")

News readers want to know 'how old/fresh is this content', rather than 'what time'.
So what about "published 2 hours ago"?

For articles, we'd better write timeless content so we provide value despite (even) what year your reader consumes it.

You just wrote such insightful article: timeless, so thank you for sharing and starting this dialogue, Mads!

Cheers,
Mike

BTW: left out my @Swatch Universal Internet Time suggestion, since I meant it as 'server-only format' instead of BE.NET's time (zone) offset workaround. Whole different story and gosh, don't want to see 'posted today at @877' on my screen ever ;)

Mike van Zandwijk Netherlands |

3/16/2008 6:06:46 PM #

Dan Atkinson

Mads, Here's n example of what I was mentioning:
www.blog.ingenuitynow.net/...DateTime+TimeAgo.aspx

Dan Atkinson United Kingdom |

3/16/2008 9:01:00 PM #

Sean

Personally I think that the concept is quite neat.  

I am not a fan of its usage anywhere where someone might even remotely care about the time though.  While, I would agree that most people probably don't care about the seconds, I for one care about the minutes. That clock from 37signals is a perfect example. If I saw that somewhere I'd be very frustrated -- is it 11:50? 11:55? 11:59?!?  

Also, the usage here on the blog makes the timestamps harder to read in my opinion -- I have to actually read them rather than just intuitively skim/process them. I think part of my aversion also stems from the fact that no one (at least not here) says <i>"at a quarter to three pm"</i> or <i>"at a quarter past midnight"</i>.  Instead they'd say <i>"at two forty-five pm"</i> or <i>"at twelve fifteen am"</i>.

All in all, regional variations, handling timezones and dealing with the multi-lingual date/time nuances make this seem like a "neat" idea but not really a "usable" one.

Sean Canada |

3/17/2008 3:18:53 PM #

pingback

Pingback from blog.cwa.me.uk

Reflective Perspective - Chris Alcock  » The Morning Brew #54

blog.cwa.me.uk |

3/17/2008 5:37:15 PM #

Derek Fowler

Nice idea but I agree with Sean, by using a representation of the time in words it means you need to cater for language rather than just whether to display a period or comma instead of a colon between the digits.

In German, for example, halb neun (half nine) actually means half to nine - 8:30 - so you'd need to cater for things like this too. You would probably end up just having text files with all the possible string time representations in, as the rules in this method would become far too complex.

Derek Fowler United Kingdom |

3/17/2008 7:02:38 PM #

pingback

Pingback from alvinashcraft.com

Dew Drop - March 17, 2008 | Alvin Ashcraft's Morning Dew

alvinashcraft.com |

3/18/2008 7:17:47 PM #

Rob W

Definitely a useful possibility to have!  Be careful about where you use it, though; it's dangerous to remove information unless you're sure you know people are not using it.

Take the example of reading blog entries & comments.  I check blog timestamps for age; if I see a technical discussion that's a few years old (they still turn up in google results...), I want to know... so it *must* contain the year or I'll be frustrated, but time of day makes little difference to me (unless I'm curious whether a lack of comments means "posted 3 minutes ago" vs. "no readers", I suppose).

I check comment timestamps for two things:
1) general comment age: if the most recent comment is weeks old, I won't bother to comment.  Your timing scheme is fine for this, but again, year is important.
2) comment sequence: are oldest or newest comments at the top?  For a busy site, the fuzzy times won't work for me... the first 10 comments could all be in the first 15 minutes, so I have to keep scrolling until I figured it out.  Not helpful.

Also, for ease of reading, I probably would stick with 11 instead of eleven... isn't it easier to read?

The important idea here for me, though, is that there are more ways to represent timestamp data than just long form vs. short form standard formatting -- including approximate, age, etc..

Rob W United States |

3/18/2008 11:12:51 PM #

Michael B

Check artlebedev (http://www.artlebedev.com/everything/verbarius/) if you want a real clock that does exactly the same thing (multi language,  config via usb)

Michael B Germany |

3/20/2008 2:33:57 AM #

Daniel

I already have an extension method for the users who are looking for "commented five seconds ago"-style relative time. I call it "freshness".

www.dimebrain.com/2008/03/fulfilling-the.html

Daniel Canada |

3/21/2008 4:28:14 PM #

jason

I agree with the "sceptics" here - As a human, I'ts much harder to process the text version than the plain old HH:MM:SS, and as pointed out by others; localization of this would be a nightmare (in norway we say "halv ti" ("half ten") for 9:30, not "half past nine"...). As an inverse example, sole numeric isn't always the best: The idea reminds me slight of the car manufacturer Opel's attempt at using digital speedometer. It was fancy, "clever" and something else, but the human brain used to much effort in processing the sole numeric display - it was sooo much easier to interpret the regular "dial" showing the current speed with a needle. A glimse at the speedometer and the brain knew immediately what the current speed approximately was, relative to the current speed limit.

For the date-formatting, I'm more a fan of the version which displays the day-part in relative text, and time as HH:MM, e.g. "Today 13:05", "Yesterday 15:53", "19. March 2008 11:09"

jason Norway |

5/27/2008 5:50:18 AM #

henke

Hiya,

Just wanted to say that in Sweden we say "halv tio", i.e. we add an extra "o" to the "ti", making our language 50% less effective than Norwegian if we want to say 10. No wonder it was the Norwegians who got the oil with their effectiveness and what not! ;)

I think what Jason is saying; generate a clock picture and be done with it!

Cheers
Henke

henke Sweden |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008