chunpin
10/7/2017 - 4:48 PM

convert the milliseconds into second, minute, hour and day

convert the milliseconds into second, minute, hour and day

function prettyPrintDate(ms){ // take milliseconds as input parameter

	var units = [
		{ ms: 1000, label: 'second' },
		{ ms: 1000 * 60, label: 'minute' },
		{ ms: 1000 * 60 * 60, label: 'hour' },
		{ ms: 1000 * 60 * 60 * 24, label: 'day' }
	],
		result,
		i,
		len,
		unit,
		label,
		suffix = 'ago';


	for(i = 0 , len = units.length; i < len; i++){
		unit = units[i];

		if( ms >= unit.ms ){
			result = ms / unit.ms;
			label = unit.label;
		}
	}


	if(result > 1){
		label += 's';
	}


	return result + ' ' + label + ' ' + suffix;

}