I got the below script to convert dates into pretty dates. It works perfectly fine but only to a level of weeks. I need it to work fine for months and perhaps years to.
How can I modify this code to work?
function prettyDate(time) {
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ){
return;
}
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}
I got the below script to convert dates into pretty dates. It works perfectly fine but only to a level of weeks. I need it to work fine for months and perhaps years to.
How can I modify this code to work?
function prettyDate(time) {
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ){
return;
}
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}
Share
Improve this question
asked Jun 1, 2011 at 20:02
Jonathan ClarkJonathan Clark
20.6k29 gold badges116 silver badges177 bronze badges
6
- 1 Need a valid "time" value. This code feels like "You touch it, you break it". – Kraz Commented Jun 1, 2011 at 20:14
- I got it here: ejohn/blog/javascript-pretty-date. I belive this is valid: 2010-01-28T20:24:17Z. – Jonathan Clark Commented Jun 1, 2011 at 20:19
- 1 @Jonathan Clark - check the ments on that page you cite for some more information. – Mark Schultheiss Commented Jun 1, 2011 at 20:22
- Especially ejohn/blog/javascript-pretty-date/#postment ment-297470 – mplungjan Commented Jun 1, 2011 at 20:31
- 1 @Matt ..click the link above, scroll down, click show ments then go to ejohn/blog/javascript-pretty-date/#ment-297470 – zack Commented Jun 1, 2011 at 20:36
3 Answers
Reset to default 8I would look at this: http://ejohn/blog/javascript-pretty-date/. Check the ments for some additional information.
See here: http://jsfiddle/MarkSchultheiss/cgMZz/1/
After a BIT more reading I see an update posted here: http://www.zachleat./web/yet-another-pretty-date-javascript/ and finally moved to: https://github./zachleat/Humane-Dates all credit to the coders!
I use a bination of a dateDiff extension for dates and this (toAge) extension:
Date.prototype.toAge = function() {
// all parisons are based on GMT time
var s = "";
var span = 0;
var uom = "";
// get the current time in GMT
var oDiff = this.dateDiff(new Date(new Date().toUTCString().replace(" UTC", "").replace(" GMT", "")));
if (oDiff.TotalMinutes < 59) {
span = Math.round(oDiff.TotalMinutes);
uom = "min";
}
else {
if (oDiff.TotalHours < 23.5) {
span = Math.round(oDiff.TotalHours);
uom = "hour";
}
else {
span = Math.round(oDiff.TotalDays);
uom = "day";
}
}
if (span < 0) span = 0;
s = span + " " + uom;
if (span > 1) s += "s";
s += " ago";
return s;
}
Try moment.js. It's brilliant for anything time related in javascript.
To do your function:
moment(time).fromNow();