showing results for - "typescript 2fjavascript time ago from datetime"
Fátima
03 Jun 2016
1/**
2 * A function that converts date object to human readable time ago
3 * @param {Date} date
4 * @returns {string}
5 * @author [YogPanjarale](https://github.com/YogPanjarale)
6 */
7 export function dateToTimeAgo(date: Date): string {
8	const now = new Date(Date.now());
9	const difftime = now.getTime() - date.getTime();
10	const diffDate = new Date(difftime - 5.5 * 60 * 60 * 1000);
11	const [sec, min, hr, day, month] = [
12		diffDate.getSeconds(),
13		diffDate.getMinutes(),
14		diffDate.getHours(),
15		diffDate.getDate() - 1,
16		diffDate.getMonth(),
17	];
18	const f = (property, end) =>{
19		// console.log(property,end)
20		return`${property} ${end}${property > 1 ? "s" : ""} ago`;
21	}
22	// console.log(diffDate.toLocaleString());
23	return month >= 1
24		? f(month, "month")
25		: day >= 1
26		? f(day, "day")
27		: hr >= 1
28		? f(hr, "hr")
29		: min >= 1
30		? f(min, "min")
31		: day >= 1
32		? f(sec, "sec")
33		: "";
34
35
36	throw new Error("Date To time ago not implmented");
37}