Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/265547-help-with-date-code/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/265547-help-with-date-code/#findComment-1360964
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/265547-help-with-date-code/#findComment-1361002
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.