JSFiddle - React, Tailwind, and code Playground
by thillin
JavaScript
/*The date heavy attempt, seems generally less acurrate suprisingly.*/
var birth = {};
birth.day = 9;
birth.month = 1;
birth.year = 1995;
var birthdate = new Date(birth.year, birth.month, birth.day);
// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() -week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);};
var current = new Date();
var currentWeeks = current.getWeek();
var currentYear = current.getFullYear();
var birthWeeks = birthdate.getWeek();
var weeksDif = ((currentYear - birth.year) * 52) + (currentWeeks - birthWeeks);
console.log(currentWeeks +', ' + currentYear);
console.log(weeksDif);