Return to Snippet

Revision: 16732
at August 13, 2009 16:14 by jonniespratley


Initial Code
function normalizeDate(time)
{
	var values = time.split(" ");
	time = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
		 delta = delta + (relative_to.getTimezoneOffset() * 60);

	var out = '';
	if (delta < 60) 
	{
		out = 'a minute ago';
	} else if(delta < 120) 
	{
		out = 'couple of minutes ago';
	} else if(delta < (45*60)) 
	{
		out = (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if(delta < (90*60)) 
	{
		out = 'an hour ago';
	} else if(delta < (24*60*60)) 
	{
		out = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if(delta < (48*60*60)) 
	{
		out = '1 day ago';
	} else 
	{
		out = (parseInt(delta / 86400)).toString() + ' days ago';
	}

		return out;
	}

Initial URL

                                

Initial Description

                                

Initial Title
Relative Date Parse

Initial Tags
date, style, twitter

Initial Language
JavaScript