Thursday, March 19, 2009

Changing YetAnotherForum to display time as "x minutes ago"

This really demonstrates the beauty of open source!

I've got a lot of international visitors to www.runsaturday.com - so I wanted to avoid time zone specific times (yes - I know YAF does let each user customise the time zone, but that doesn't really help when I get so many guests through)

So ... I decided to adopt the facebook/twitter approach - to listing times like "23 seconds ago" and "in the last week".

It was remarkable easy to change. Here's the main new code:

        /// <summary>

        /// Formats a datetime value into "friendly terms" - let's hope this works!

        /// the date is yesterday or today -- in which case it says that.

        /// </summary>

        /// <param name="o">The datetime to be formatted</param>

        /// <returns>Formatted string of DateTime object</returns>

        public string FormatDateTimeTopic(object o)

        {

            //string strDateFormat;

            DateTime dt = Convert.ToDateTime(o) +TimeOffset;

            DateTime nt = DateTime.Now+TimeOffset;

 

            TimeSpan diff = nt - dt;

            double totalSeconds = diff.TotalSeconds;

            double totalMinutes = diff.TotalMinutes;

            double totalHours = diff.TotalHours;

            double totalDays = diff.TotalDays;

 

            try

            {

                if (totalSeconds < 15.0)

                {

                    return GetText("MomentsAgo");

                }

                if (totalSeconds < 100.0)

                {

                    return string.Format(GetText("SecondsAgo"), totalSeconds);

                }

                if (totalMinutes < 100.0)

                {

                    return string.Format(GetText("MinutesAgo"), totalMinutes);

                }

                else if (totalHours < 10.0)

                {

                    return string.Format(GetText("HoursAgo"), totalHours);

                }

                else if (totalDays < 1.0)

                {

                    return GetText("InTheLastDay");

                }

                else if (totalDays < 2.0)

                {

                    return string.Format(GetText("ADayAgo"), totalDays);

                }

                else if (totalDays < 30.0)

                {

                    return string.Format(GetText("DaysAgo"), totalDays);

                }

                else

                {

                    return dt.Date.ToString("dd MMM yy");

                }

            }

            catch (Exception)

            {

                return dt.ToString("f");

            }

        }

 

Old code was:



            /// <summary>

            /// Formats a datatime value into 07.03.2003 00:00:00 except if

            /// the date is yesterday or today -- in which case it says that.

            /// </summary>

            /// <param name="o">The datetime to be formatted</param>

            /// <returns>Formatted string of DateTime object</returns>

            public string FormatDateTimeTopicOld( object o )

            {

                  string strDateFormat;

                  DateTime dt = Convert.ToDateTime( o ) + TimeOffset;

                  DateTime nt = DateTime.Now + TimeOffset;

 

                  try

                  {

                        if ( dt.Date == nt.Date )

                        {

                              // today

                              strDateFormat = String.Format( GetText( "TodayAt" ), dt );

                        }

                        else if ( dt.Date == nt.AddDays( -1 ).Date )

                        {

                              // yesterday

                              strDateFormat = String.Format( GetText( "YesterdayAt" ), dt );

                        }

                        else if ( BoardSettings.DateFormatFromLanguage )

                        {

                              strDateFormat = dt.ToString( GetText( "FORMAT_DATE_TIME_SHORT" ) );

                        }

                        else

                        {

                              strDateFormat = String.Format( "{0:f}", dt );

                        }

                        return strDateFormat;

                  }

                  catch ( Exception )

                  {

                        return dt.ToString( "f" );

                  }

            }

 

No comments:

Post a Comment