Hi micah,
Please refer below sample.
HTML
<asp:Repeater ID="rptDate" runat="server" OnItemDataBound="rptDate_ItemDataBound">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th>
Date
</th>
<th>
Message
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Date") %>' />
</td>
<td>
<asp:Label ID="lblMessage" runat="server" Text='<%# Eval("Message") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Date", typeof(DateTime)), new DataColumn("Message", typeof(string)) });
dt.Rows.Add("07/14/2018 11:45");
dt.Rows.Add("07/16/2018 18:25");
dt.Rows.Add("06/16/2018 18:25");
dt.Rows.Add("06/16/2016 18:25");
rptDate.DataSource = dt;
rptDate.DataBind();
}
protected void rptDate_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblDate = (Label)e.Item.FindControl("lblCountry");
((Label)e.Item.FindControl("lblMessage")).Text = CalCulateTime(Convert.ToDateTime(lblDate.Text));
}
}
public string CalCulateTime(DateTime postDate)
{
string message = "";
DateTime currentDate = DateTime.Now;
TimeSpan timegap = currentDate - postDate;
message = string.Concat("Posted on ", postDate.ToString("MMMM dd, yyyy"), " at ", postDate.ToString("hh:mm tt"));
if (timegap.Days > 365)
{
message = string.Concat("Posted ", (((timegap.Days) / 30) / 12), " years/s ago");
}
else if (timegap.Days > 31)
{
message = string.Concat("Posted ", ((timegap.Days) / 30), " month/s ago");
}
else if (timegap.Days > 1)
{
message = string.Concat("Posted ", timegap.Days, " day/s ago");
}
else if (timegap.Days == 1)
{
message = "Posted yesterday";
}
else if (timegap.Hours >= 2)
{
message = string.Concat("Posted ", timegap.Hours, " hour/s ago");
}
else if (timegap.Hours >= 1)
{
message = "Posted an hour ago";
}
else if (timegap.Minutes >= 60)
{
message = "Posted more than an hour ago";
}
else if (timegap.Minutes >= 5)
{
message = string.Concat("Posted ", timegap.Minutes, " minute/s ago");
}
else if (timegap.Minutes >= 1)
{
message = "Posted a few minute/s ago";
}
else
{
message = "Posted less than a minute ago";
}
return message;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Date", GetType(DateTime)), New DataColumn("Message", GetType(String))})
dt.Rows.Add("07/14/2018 11:45")
dt.Rows.Add("07/16/2018 18:25")
dt.Rows.Add("06/16/2018 18:25")
dt.Rows.Add("06/16/2016 18:25")
rptDate.DataSource = dt
rptDate.DataBind()
End Sub
Protected Sub rptDate_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lblDate As Label = CType(e.Item.FindControl("lblCountry"), Label)
CType(e.Item.FindControl("lblMessage"), Label).Text = CalCulateTime(Convert.ToDateTime(lblDate.Text))
End If
End Sub
Public Function CalCulateTime(ByVal postDate As DateTime) As String
Dim message As String = ""
Dim currentDate As DateTime = DateTime.Now
Dim timegap As TimeSpan = currentDate - postDate
message = String.Concat("Posted on ", postDate.ToString("MMMM dd, yyyy"), " at ", postDate.ToString("hh:mm tt"))
If timegap.Days > 365 Then
message = String.Concat("Posted ", (((timegap.Days) / 30) / 12), " years/s ago")
ElseIf timegap.Days > 31 Then
message = String.Concat("Posted ", ((timegap.Days) / 30), " month/s ago")
ElseIf timegap.Days > 1 Then
message = String.Concat("Posted ", timegap.Days, " day/s ago")
ElseIf timegap.Days = 1 Then
message = "Posted yesterday"
ElseIf timegap.Hours >= 2 Then
message = String.Concat("Posted ", timegap.Hours, " hour/s ago")
ElseIf timegap.Hours >= 1 Then
message = "Posted an hour ago"
ElseIf timegap.Minutes >= 60 Then
message = "Posted more than an hour ago"
ElseIf timegap.Minutes >= 5 Then
message = String.Concat("Posted ", timegap.Minutes, " minute/s ago")
ElseIf timegap.Minutes >= 1 Then
message = "Posted a few minute/s ago"
Else
message = "Posted less than a minute ago"
End If
Return message
End Function
Screenshot