JohnnyDoomo Posted July 11, 2012 Share Posted July 11, 2012 I have this code in a javascript and I am trying to make a change to it. function nicetime(a,out){ var c = a.length > 1 ? a : new Date; var d = Math.floor(parseInt((c.getTime() - a) / 1e3),10); d = d < 0 ? d + -(c.getTimezoneOffset()*60) : d ; if(out == 0){ var chunks = new Array(); chunks[0] = [60 * 60 * 24 * 365 , 'year']; chunks[1] = [60 * 60 * 24 * 30 , 'month']; chunks[2] = [60 * 60 * 24 * 7, 'week']; chunks[3] = [60 * 60 * 24 , 'day']; chunks[4] = [60 * 60 , 'hr']; chunks[5] = [60 , 'min']; var i = 0; var j = chunks.length; for (i = 0; i < j; i++) { s = chunks[i][0]; n = chunks[i][1]; if ((xj = Math.floor(d / s)) != 0) break; } val = xj == 1 ? '1 '+n : xj+' '+n+'s'; if (i + 1 < j) { s2 = chunks[i + 1][0]; n2 = chunks[i + 1][1]; if ( ((xj2 = Math.floor((d - (s * xj)) / s2)) != 0) ) val += (xj2 == 1) ? ' + 1 '+n2 : ' + '+xj2+' '+n2+'s'; } val += ' ago'; return val; } else { return d; } } I am not a coder, but I am trying to get it so that if it's going to display a date that has a year in it, instead of doing what it's currently doing (Showing a date of "-1 years + 12 months ago") is says something like "Now" or "5 Minutes ago". For some reason the above code is showing the "-1 years + 12 months ago" on the latest additions using the script. I am looking for a patch to simply replace it if it is going to display the year date, or if you see why the code is displaying the "-1 years + 12 months ago" in the first place on the most recent items the script is showing the date for, and can fix it that would be appreciated. Thanks for any help you can provide. Quote Link to comment https://forums.phpfreaks.com/topic/265547-help-with-date-code/ Share on other sites More sharing options...
requinix Posted July 12, 2012 Share Posted July 12, 2012 I kiiinda rewrote it. Felt like it. function nicetime(a, out) { a = parseInt(a instanceof Date ? a.getTime() : a); var now = new Date(); var d = Math.floor((now.getTime() - a) / 1000); if (!out) { return d; } if (d return "Now"; } var chunks = [ [60*60*24*365, 'year', 'years'], [60*60*24*30, 'month', 'months'], [60*60*24*7, 'week', 'weeks'], [60*60*24, 'day', 'days'], [60*60, 'hr', 'hrs'], [60, 'min', 'mins'], [1, 'sec', 'secs'] ]; var nice = []; for (var i = 0; i if (d >= chunks[i][0]) { var count = Math.floor(d / chunks[i][0]); nice.push("" + count + " " + chunks[i][count == 1 ? 1 : 2]); d -= count * chunks[i][0]; } } return nice.join(" ") + " ago"; } I doubt that's exactly what you want but it should be close. Quote Link to comment https://forums.phpfreaks.com/topic/265547-help-with-date-code/#findComment-1360964 Share on other sites More sharing options...
JohnnyDoomo Posted July 12, 2012 Author Share Posted July 12, 2012 That completely breaks the format. Instead of something like "3 hrs + 56 mins ago" I not get "6026". Quote Link to comment https://forums.phpfreaks.com/topic/265547-help-with-date-code/#findComment-1361001 Share on other sites More sharing options...
requinix Posted July 12, 2012 Share Posted July 12, 2012 I don't get 6026 either. I didn't know what out was supposed to be so I just guessed at a boolean. Try if (out != 0) { Also I didn't put in the timezone stuff because I wasn't even sure you needed/should have it. Feel free to put it back. And for the record, as an example nicetime(new Date(2012, 0, 1, 0, 0, 0, 0), true) Quote Link to comment https://forums.phpfreaks.com/topic/265547-help-with-date-code/#findComment-1361002 Share on other sites More sharing options...
JohnnyDoomo Posted July 13, 2012 Author Share Posted July 13, 2012 Thank you, that fixed my problem. Quote Link to comment https://forums.phpfreaks.com/topic/265547-help-with-date-code/#findComment-1361196 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.