nblackburn
11/5/2017 - 11:55 AM

Format milliseconds to short time

Formats a millisecond timestamp to the highest unit of order.

module.exports = milliseconds => {
	let final = '';
	let seconds = Math.floor(milliseconds / 1000);
	let minutes = Math.floor(seconds / 60);
	let hours = Math.floor(minutes / 60);

	let units = [
		{unit: 'h', value: hours},
		{unit: 'm', value: minutes % 60},
		{unit: 's', value: seconds % 60},
		{unit: 'ms', value: milliseconds % 1000},
	];

	units.filter(i => i.value > 0).forEach(current => {
		final += current.value + current.unit;
	});

	return final;
};