0

I have stored RSS feed's published date "pubDate" in a JS string variable. Now I need to show it in a more readable way like "2 seconds ago, 5 minutes ago, an hour ago, 5 days ago, a month ago, 1 year ago" like in PrettyDate, MomentJS. It should be light weight. And shouldn't use external JS plugins.

I can change the initial date string using toGMTString(), toISOString(), toString() or toLocaleString(). How can I do this?

Teshan N.
  • 2,307
  • 3
  • 30
  • 59
  • 2
    Please include some attempted solutions, why they didn't work, and the expected results. – palaѕн Oct 21 '13 at 05:44
  • Using "time ago" isn't more readable for some (many?). Pretty simple to compare the current time with another and determine what to display based on the difference. – RobG Oct 21 '13 at 05:47
  • 1
    Check this...http://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site – Nikhil N Oct 21 '13 at 05:59

3 Answers3

0

May be this help u out, use it acc. to ur req. :

var dtDate1 = "2013-10-21 13:45:06";

var dtDate2 = dtDate1.replace(/-/g,'/');

var nDifference = Math.abs(new Date() - new Date(dtDate2));

alert('Difference in milliseconds : ' + nDifference);

alert('time diff in hr : ' + Math.round(nDifference/3600000));

alert('time diff in min : ' + Math.round(nDifference/60000));

alert('time diff in sec : ' + Math.round(nDifference/1000));

alert('Difference in days : ' + Math.round(nDifference/86400000));

0

There are a wide range of Javascript and Jquery libraries available if for handling date easily ! So I would suggest you to go for that only as it would make your work a bit easier. You can refer this link for a list available libraries :

http://codegeekz.com/6-javascript-date-libraries-for-developers/

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
0

how much lightweight do you need ?

MomentJS is pretty good lib but still like you said, it tries to solve many problems. The best strategy is trying to use it first, then if you find any performance throttle ( after profiling ofc ), then you can try to pick the part you need.

This works best for me when I need something specific.

Dzung Nguyen
  • 9,152
  • 14
  • 65
  • 104
  • I just need to show a time int he whole app. SO it is not required to add a whole big set of codes to the app to view a simple portion of text (a formatted time text). That is why I need a simple hand written code. – Teshan N. Oct 21 '13 at 17:03
  • yeah, like I said you should go there and pick the function that solve the specific input. You should specify what is JS string variable in your case. It'd be helpful. – Dzung Nguyen Oct 22 '13 at 15:43