Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

typescript/JavaScript time ago from datetime

/**
 * A function that converts date object to human readable time ago
 * @param {Date} date
 * @returns {string}
 * @author [YogPanjarale](https://github.com/YogPanjarale)
 */
 export function dateToTimeAgo(date: Date): string {
	const now = new Date(Date.now());
	const difftime = now.getTime() - date.getTime();
	const diffDate = new Date(difftime - 5.5 * 60 * 60 * 1000);
	const [sec, min, hr, day, month] = [
		diffDate.getSeconds(),
		diffDate.getMinutes(),
		diffDate.getHours(),
		diffDate.getDate() - 1,
		diffDate.getMonth(),
	];
	const f = (property, end) =>{
		// console.log(property,end)
		return`${property} ${end}${property > 1 ? "s" : ""} ago`;
	}
	// console.log(diffDate.toLocaleString());
	return month >= 1
		? f(month, "month")
		: day >= 1
		? f(day, "day")
		: hr >= 1
		? f(hr, "hr")
		: min >= 1
		? f(min, "min")
		: day >= 1
		? f(sec, "sec")
		: "";


	throw new Error("Date To time ago not implmented");
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert timestamp to time javascript 
Javascript :: express url redirect 
Javascript :: rock paper scissors javascript 
Javascript :: angular remove index of array 
Javascript :: javascript function uppercase to lowercase 
Javascript :: comments js 
Javascript :: add element into array 
Javascript :: type svg react 
Javascript :: how to make text channels in discord.js 
Javascript :: Check for a Null or Empty String in JavaScript 
Javascript :: click on button submitting the form in angular 
Javascript :: javascript date format 
Javascript :: javascript push object into array with variable key 
Javascript :: match ids from 2 arrays in javascript asynchronous programming 
Javascript :: log javascript 
Javascript :: javascript get form input data 
Javascript :: redux dev tool 
Javascript :: get ini file with node js 
Javascript :: create new angular project specific version 
Javascript :: static variable in javascript 
Javascript :: next.js index page 
Javascript :: React tagInput component 
Javascript :: angular create library 
Javascript :: axios get method 
Javascript :: js match any number string 
Javascript :: accessing via name jquery 
Javascript :: razor list to js array 
Javascript :: javascript push array with key name 
Javascript :: nodejs watermark image 
Javascript :: lodash merge 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =