Can anyone help me modify Andris’ solution from this post: Convert seconds to days, hours, minutes and seconds to also include years, months, and weeks?
I am currently running this code:
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);
}
Since 06-06-2020, exactly 60 days have passed up to now.
The output shows "2 months, 4 weeks, 4 days, 21 minutes, 10 seconds" which seems incorrect. It should be more like "2 months, 21 minutes, 10 seconds."
I'm struggling with another date, January 15, 2020, where 51 days have elapsed. However, the result is "1 month, 2 weeks, 2 days, 41 minutes, 43 seconds," totaling 46 days instead of the correct "1 month, 3 weeks, 0 days, 41 minutes, 43 seconds."
Thank you so much for any assistance!