aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

JQuery display time in facebook/ twitter formats like minute ago, hour ago, week ago in

Feb 16, 2012

In this article I will explain how to show time in facebook/ twitter formats like minute ago, hour ago, week ago in using JQuery.

In previous posts I explained many articles relating to
JQuery. Now I will explain another article relating to JQuery. If we share anything on facebook or twitter we will get time in different format like 1 minute ago, 1 hour ago, 1 week ago etc.
To implement this concept I am using previous post Repeater example in After bind data to repeater control our date and time would be like this
We can change date and time format like facebook / twitter style (minute ago, hour ago, month ago etc) by using JQuery timeago plugin. This plugin automatically update our datetime without having any postback of page you can get this JQuery timeago plugin by downloading attached folder.
Before use this plugin I used other methods to change date and time format like facebook/twitter but I got one problem that is now I opened website during that time that is showing 1 minute ago. I stayed on that website upto 5 minutes but still that is showing 1 minute only if I refresh website then datetime value is updating but this JQuery timeago plugin automatically update our datetime without having any postsbacks.
To implement this one check previous post Repeater example in I am using same concept just adding new logic to change date and time format
After design data table and insert data in database by using above repeater post write the following code in your aspx page  to bind data o repeater control

<html xmlns="">
<head id="Head1" runat="server">
<title>Sample to Display time in facebook/twitter format like 1 minute ago, 1 hour ago, 1 week ago, 1 month ago</title>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.timeago.js" type="text/javascript"></script>
<script src="js/test_helpers.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {

<form id="form1" runat="server">
<asp:Repeater ID="RepDetails" runat="server">
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:#df5015; color:White">
<td colspan="2">
<tr style="background-color:#EBEFF0">
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Subject") %>' Font-Bold="true"/>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment") %>'/>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("UserName") %>'/></td>
<td>Created Date: <label class="timeago" title="<%#Eval("PostedDate") %>" style=" font-weight:bold"></label></td>
<td colspan="2">&nbsp;</td>
If you observe above code in header section I added some of script files by using those files we can change date and time. To get those files download attached sample code and use it in your application.

Another thing here we need to know is label link

<label class="timeago" title="<%#Eval("PostedDate") %>" style=" font-weight:bold"/>
JQuery timeago plugin automatically change time format for label elements with class="timeago" and title.

After completion of aspx page design add following namespaces in code behind

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
After add namespace write the following code

private SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true");
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
//Bind Data to Repeater Control
protected void BindRepeaterData()
SqlCommand cmd = new SqlCommand("select * from Repeater_Table Order By PostedDate desc", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (ds.Tables[0].Rows.Count > 0)
RepDetails.Visible = true;
RepDetails.DataSource = ds;
RepDetails.Visible = false;


Imports System.Data
Imports System.Data.SqlClient
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page
Private con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
End If
End Sub
Protected Sub BindDataList()
'Query to get ImagesName and Description from database
Dim command As New SqlCommand("SELECT ImageName,Description from SlideShowTable", con)
Dim da As New SqlDataAdapter(command)
Dim dt As New DataTable()
dlImages.DataSource = dt
End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
'Get Filename from fileupload control
Dim filename As String = Path.GetFileName(fileuploadimages.PostedFile.FileName)
'Save images into SlideImages folder
fileuploadimages.SaveAs(Server.MapPath("SlideImages/" & filename))
'Open the database connection
'Query to insert images name and Description into database
Dim cmd As New SqlCommand("Insert into SlideShowTable(ImageName,Description) values(@ImageName,@Description)", con)
'Passing parameters to query
cmd.Parameters.AddWithValue("@ImageName", filename)
cmd.Parameters.AddWithValue("@Description", txtDesc.Text)
'Close dbconnection
txtDesc.Text = String.Empty
End Sub
End Class
Now run your application check the output that would be like this   

After open your website stay on that website for 1 or 2 minutes and check your time in website that will be updated automatically.

Download sample code attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...


imran said...


Anonymous said...

Suresh Sir
AAp ek code post kar sakte hai kya?
Google Map (API) Jis city ko dikna chaye use page load par dal kar dekha sakte hai in code
Plz post this I need This Code ........................

sagar said...


kya aap facebook type comment box ka codding bata sakte he....agar he to kya name he vo bata ye ..
plz i need your help......

Anonymous said...

excellent code..

Pankaj Kumar said...

Jquery of to show time in facebook/ twitter formats like minute ago, hour ago, week ago in using JQuery.
not working properly.....

Suresh Dasari said...

@Pankaj Kumar..
it's working perfectly. Please check your code

Santosh Pal said...

JQuery display time in facebook/ twitter formats like minute ago, hour ago, week ago in is not working properly.when i run this code its show Created Date: 4 months ago for all.

Suresh Dasari said...

@Santosh Pal...
Check your created dates range based on that only it will show..

Santosh Pal said...

Suresh Sir i have created 1 table and i have some data like
Id UserName Subject Comment PostedDate
11 sarfaraz Check ssjjfj 2012-06-01 16:26:00.000
12 santoshpal Pal good 2012-06-01 16:52:00.000
13 minal Check hi 2012-06-01 17:13:00.000

when i run my code its show Created Date: 4 months ago for all.
what i have to do extra things to get result
Plz help me sir.

Suresh Dasari said...

@Santhosh Pal...
Please change your date format to mm/date/year like 06/01/2012 then try it will work for you.

Santosh Pal said...

Thanks to his help Suresh sir

Vidit Tyagi said...

Its working perfectly but when we add new comment in a post all dates automatically hidden becoz at that moment ur jquery is not working properly becoz of data binding so i think

var ts = new TimeSpan(DateTime.UtcNow.Ticks - dt.Ticks);
double delta = Math.Abs(ts.TotalSeconds);

if (delta < 60)
return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
if (delta < 120)
return "a minute ago";
if (delta < 2700) // 45 * 60
return ts.Minutes + " minutes ago";
if (delta < 5400) // 90 * 60
return "an hour ago";
if (delta < 86400) // 24 * 60 * 60
return ts.Hours + " hours ago";
if (delta < 172800) // 48 * 60 * 60
return "yesterday";
if (delta < 2592000) // 30 * 24 * 60 * 60
return ts.Days + " days ago";
if (delta < 31104000) // 12 * 30 * 24 * 60 * 60
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
return months <= 1 ? "one month ago" : months + " months ago";
int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years <= 1 ? "one year ago" : years + " years ago";

this is much better thn ur code

Suresh Dasari said...

Whenver you enter comment and hidding date means that's the problem with your code. Please check it that's not the problem with this plugin.

Anonymous said...

Thanks in a million!!!!!! it really works i will observe this if the date of the client pc is not update to date

Anonymous said...

Hello Sir,
I have used this code and set date time format (MM/dd/YYYY) but it shows 14 hours ago ,17 hours ago like this for all records. please help thanks

Anonymous said...

how display twitter user comments in

Anonymous said...

hi suresh garu

it is not working server side because label have no runat="server" wat we do.......

Anonymous said...

vcn bvn

Manjusha said...

Nice one..


Hello Suresh Dasari

I have added timeago plugin in my web application.It is similar post and comment system like facebook.After each post and comment i do a partial post back with ajax and bind the data back to repeater.But after the partial post back the time ago do not bind .If I remove update panel and use full post back it works fine.Pls help,seeking your help.

Nitin Goswami

Anonymous said...

Hi Suresh,

Thanks for your code,i have one doubt that can we implement this same concept in the datalist. If yes,How to You..

Anonymous said...

I got an answer for this.,thnks to use this same concept in data list..thnks for your code in advance.

Anonymous said...

hello Suresh
Its not working if we do partial post.I mean if we put some code with in updatepanel and doing operation then its not working.all the previous data is hidden.

Anonymous said...

I have created 3 linkbutton and each link button binded with listview,but on every click of linkbutton the time ago is not working.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.