Im trying something like Andris answer in this post: Convert seconds to days, hours, minutes and seconds but i need this in years, month and weeks as well.
im running this
getDateStrings() {
console.log(req_creation_date);
const today = new Date();
const creation_date = new Date('2020-01-06T20:24:00.000Z');
const creation_date_diff = Math.abs(today.getTime() - creation_date.getTime());
const creation_date_diffDays = Math.ceil((creation_date_diff / 1000));
console.log(creation_date_diffDays);
const creation_date_diffDays_days = Math.ceil((creation_date_diff / (1000 * 3600 * 24)) - 1);
console.log(creation_date_diffDays_days);
const y = Math.floor(creation_date_diffDays / (31536000));
const ms = Math.floor(creation_date_diffDays % (3600 * 24 * 7 * 4.34524 * 12) / 2592000);
const w = Math.floor(creation_date_diffDays % (3600 * 24 * 7 * 4.34524) / 604800);
const d = Math.floor(creation_date_diffDays % (3600 * 24 * 7) / 86400);
const h = Math.floor(creation_date_diffDays % (3600 * 24) / 3600);
const m = Math.floor(creation_date_diffDays % 3600 / 60);
const s = Math.floor(creation_date_diffDays % 60);
const yDisplay = y > 0 ? y + (y === 1 ? ' year, ' : ' years, ') : '';
const msDisplay = ms > 0 ? ms + (ms === 1 ? ' month, ' : ' months, ') : '';
const wDisplay = w > 0 ? w + (w === 1 ? ' week, ' : ' weeks, ') : '';
const dDisplay = d > 0 ? d + (d === 1 ? ' day, ' : ' days, ') : '';
const hDisplay = h > 0 ? h + (h === 1 ? ' hour, ' : ' hours, ') : '';
const mDisplay = m > 0 ? m + (m === 1 ? ' minute, ' : ' minutes, ') : '';
const sDisplay = s > 0 ? s + (s === 1 ? ' second ' : ' seconds') : '' ;
console.log(yDisplay, msDisplay, wDisplay, dDisplay + hDisplay + mDisplay + sDisplay);
}
60 days have passed since 06-06-2020 until today.
It is returning "2 months, 4 weeks, 4 days, 21 minutes, 10 seconds" what is wrong, should be something like "2 months, 21 minutes, 10 seconds"
I don't know what I'm doing wrong, I'm trying also with January-15-2020. 51 days have passed but it is returning "1 month, 2 weeks, 2 days, 41 minutes, 43 seconds" what is in total 46 days and not 51 as it should be "1 month, 3 weeks, 0 days, 41 minutes, 43 seconds"
thank you very much!