Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I convert year to seconds?

I will prompt the user to enter the years he lived then it should return the seconds he lived.

The code:

let use = prompt("Enter number of years u lived: ");
let u = parseInt(use)
let time = new Date() 
let compute = time.getFullYear() - u



console.log(compute.getSeconds()+time.getFullYear());

But I get an error that says ‘Uncaught TypeError: compute.getSeconds is not a function’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming an approximation is fine (because you’re not asking for a birth date anyway), it might be the easiest to just multiply the number by the number of seconds per year.

const seconds = years * 60 * 60 * 24 * 365;

This is not exact because it’s not accounting for leap day and leap second rules.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading