0

On youtube you often don't get the actual upload date, it just says

uploaded x ys ago 

so how can I achieve this in javascript? Just generate a friendly string from a date?

user2741831
  • 2,120
  • 2
  • 22
  • 43

2 Answers2

1

You can achieve this with Moment.js

moment("20120620", "YYYYMMDD").fromNow();

Result will be:

7 years ago

Danilo Körber
  • 858
  • 1
  • 7
  • 27
0

Just stole this from https://www.w3resource.com/javascript-exercises/javascript-date-exercise-49.php but this looks like what you are looking for

function diff_years(dt2, dt1) 
 {

  var diff =(dt2.getTime() - dt1.getTime()) / 1000;
   diff /= (60 * 60 * 24);
  return Math.abs(Math.round(diff/365.25));

 }

dt1 = new Date(2014,10,2);
dt2 = new Date(2014,10,11);
console.log(diff_years(dt1, dt2));

dt1 = new Date("June 13, 2014 08:11:00");
dt2 = new Date("October 19, 2017 11:13:00");
console.log("uploaded " + diff_years(dt1, dt2));
Carr O'Connor
  • 139
  • 10