Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

transform date to relative date js

const delta = Math.round((+new Date - date) / 1000);

const minute = 60,
    hour = minute * 60,
    day = hour * 24,
    week = day * 7;

let fuzzy;

if (delta < 30) {
    fuzzy = 'just then.';
} else if (delta < minute) {
    fuzzy = delta + ' seconds ago.';
} else if (delta < 2 * minute) {
    fuzzy = 'a minute ago.'
} else if (delta < hour) {
    fuzzy = Math.floor(delta / minute) + ' minutes ago.';
} else if (Math.floor(delta / hour) == 1) {
    fuzzy = '1 hour ago.'
} else if (delta < day) {
    fuzzy = Math.floor(delta / hour) + ' hours ago.';
} else if (delta < day * 2) {
    fuzzy = 'yesterday';
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: using python with javascript 
Javascript :: how to recognize an array in javascript 
Javascript :: send multipart form data axios with node js 
Javascript :: date format in jquery 
Javascript :: mongoose create 
Javascript :: axios defaults headers common 
Javascript :: invariant failed you should not use link outside a router test 
Javascript :: range command in JS 
Javascript :: detect form input changes javascript 
Javascript :: checkbox react 
Javascript :: reverse a string javascript 
Javascript :: jquery moment js 
Javascript :: function declaration and function definition in javascript 
Javascript :: vue boolean 
Javascript :: convert data image url into an image file 
Javascript :: implement singleton javascript 
Javascript :: jquey datatables 
Javascript :: chrome block javascript alert 
Javascript :: how to create password generator in react 
Javascript :: javascript how to open a file 
Javascript :: check if browser supports Local Storage 
Javascript :: how to restablished closed rxjs websocket 
Javascript :: learn nodejs 
Javascript :: angular material datepicker range get value 
Javascript :: return inside for loop javascript 
Javascript :: js concat 
Javascript :: how to redirect to another page in react js on button click 
Javascript :: for of in js or for in loop in js 
Javascript :: react usememo 
Javascript :: cek versi node js 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =