views:

89

answers:

4

I have two functions written in VB.NET:

1) The first function (call it GetValues()) returns values in a SQL database as a list via a stored proc. One of the fields is DateTimeSubmitted. All of this data is displayed on my webpage using a repeater control. It works fine.

2) The second function (call it NiceDate()) I created to change the DateTimeSubmitted to a nice date such as "one minute ago" or "yesterday".

My problem is I have no idea which is the best part of the process to do the conversion (at point of data extraction or in pageload etc.) or in fact how to do the conversion.

I have tried a few things in the page load and started looking at doing the conversion with a FOR EACH loop but so far no dice.

Am I on the right track or is there an easier way?

+1  A: 

In my opinion, this is strictly view logic, so it should be handled as close to the view as possible. I couldn't figure out a clever way to do it, but this should give you a start. There're more work to do, error handling, correct cardinality of the return value (hour versus hours), but it's a proof of concept. :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DT
{
    class Program
    {
        static void Main(string[] args)
        {
            var dt1 = DateTime.Now - TimeSpan.FromDays(1);
            var dt2 = DateTime.Now - TimeSpan.FromHours(2);
            var dt3 = DateTime.Now - TimeSpan.FromMinutes(10);

            Console.WriteLine(FriendlyDate(dt1));
            Console.WriteLine(FriendlyDate(dt2));
            Console.WriteLine(FriendlyDate(dt3));

            Console.ReadLine();
        }

        private static string FriendlyDate(DateTime dt)
        {
            var cur = DateTime.Now;
            var ts = cur - dt;

            return FriendlyDate(ts);
        }

        private static string FriendlyDate(TimeSpan ts)
        {
            string retVal = String.Empty;

            if (ts.Days > 0)
            {
                retVal = String.Format("{0} day(s) ago.", ts.Days);
            }
            else if (ts.Hours > 0)
            {
                retVal = String.Format("{0} hours(s) ago.", ts.Hours);
            }
            else
            {
                retVal = String.Format("{0} minute(s) ago.", ts.Minutes);
            }

            return retVal;
        }
    }
}
JP Alioto
+1  A: 

Transforms like this can be handled in your business/service tier, whatever sits between your data access and presentation layers.

Or if this is strictly for presentation you can make a method in your codebehind and call it with your data class to get the appropriate date for display in the item data bind event.

blu
+3  A: 

You could handle the Repeater's ItemDataBound Event. From there you would use FindControl to get the control (a label perhaps?) and update it accordingly using your NiceDate() logic.

Similar to the code from the above link, you could do something like:

EDIT: updated to VB.NET

Sub myRepeater_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs)
    ' This event is raised for the header, the footer, separators, and items.

    ' Check the type: operate on Items and Alternating Items only.
    If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
        ' Get a reference to the date label then update its value
        Dim lblDate As Label = CType(e.Item.FindControl("lblDate"), Label)
        lblDate.Text = NiceDate(lblDate.Text)
    End If
End Sub
Ahmad Mageed
+1  A: 

Hi,

If you are using .Net 3.5 and if in any case you are using binding to display database values in UI elements then you can create "Converters". This is exactly for this purpose only or even if you don't want to create converter and if you are using property class then you can convert it before property get statements.

Harryboy