Click here to Skip to main content
12,693,244 members (32,191 online)
Click here to Skip to main content
Add your own
alternative version

Stats

8.2K views
13 bookmarked
Posted

How To Convert Date Time to “X minutes ago” in jQuery

, 5 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to convert date time to "X minutes ago" in jQuery

Today, I found a nice plugin with the help of which you can convert any date time on your HTML page to something similar to Gmail/Facebook updates – “5 minutes ago” or “a day ago”. The best part of this plugin is it auto updates the minutes as you’re on the webpage. So, if you have opened the page now and the date shows “1 minute ago”, after 5 minutes the same date will auto update to “6 minutes ago”. In this post, we will see how this plugin works and write a simple HTML using the plugin.

To start with, you can download the timeago plugin from here. This plugin depends on jquery so the very first thing is to include jQuery in our code and then the plugin JavaScript.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="jquery.timeago.js"></script>

Once the plugin files are loaded, you can use the plugin’s timeago() function in any of the following ways:

var date = $.timeago(new Date()); //Displays 'less than a minute ago'
var date = $.timeago('2014-05-04'); //Displays 'a day ago'

You can also call the timeago() function on a specific class on your page as below:

//This will modify the date time on all elements having class as 'time'
$(document).ready(function(){
   $(".time").timeago();
});

Keep learning and sharing! Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nitesh Luharuka
Team Leader Director @ Rebin Infotech
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.

I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralShorter and simpler Pin
Gaurav.Kumar.Arora27-Oct-14 11:32
memberGaurav.Kumar.Arora27-Oct-14 11:32 
GeneralRe: Shorter and simpler Pin
Nitesh Luharuka30-Oct-14 18:34
memberNitesh Luharuka30-Oct-14 18:34 
Questionnon jquery version? Pin
cpGlenn6-May-14 18:54
membercpGlenn6-May-14 18:54 
AnswerRe: non jquery version? Pin
Nitesh Luharuka6-May-14 19:02
memberNitesh Luharuka6-May-14 19:02 
AnswerRe: non jquery version? Pin
Nitesh Luharuka7-May-14 19:14
memberNitesh Luharuka7-May-14 19:14 
GeneralRe: non jquery version? Pin
cpGlenn7-May-14 19:22
membercpGlenn7-May-14 19:22 
GeneralRe: non jquery version? Pin
Nitesh Luharuka7-May-14 20:04
memberNitesh Luharuka7-May-14 20:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 5 May 2014
Article Copyright 2014 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid