- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Converting days into years months and weeks - JavaScript
We are required to write a JavaScript function that takes in a number (representing the number of days) and returns an object with three properties, namely −
weeks, months, years, days
And the properties should have proper values of these four properties that can be made from the number of days. We should not consider leap years here and consider all years to have 365 days.
For example −
If the input is 738, then the output should be −
const output = { years: 2, months: 0, weeks: 1, days: 1 }
Example
Let’s write the code for this function −
const days = 738; const calculateTimimg = d => { let months = 0, years = 0, days = 0, weeks = 0; while(d){ if(d >= 365){ years++; d -= 365; }else if(d >= 30){ months++; d -= 30; }else if(d >= 7){ weeks++; d -= 7; }else{ days++; d--; } }; return { years, months, weeks, days }; }; console.log(calculateTimimg(days));
Output
The output in the console: −
{ years: 2, months: 0, weeks: 1, days: 1 }
- Related Articles
- Convert:a. 60 hours into days and hoursb. 50 months into years and months
- Calculate the difference between two dates in days, weeks, months and years in Excel
- Converting seconds in years days hours and minutes in JavaScript
- Program to convert given number of days in terms of Years, Weeks and Days in C
- C program to convert days into months and number of days
- How to get days, months and years between two Java LocalDate?
- Add the following:7 years 8 months and 8 years 9 months
- Converting seconds into days, hours, minutes and seconds in C++
- Converting humanYears into catYears and dogYears in JavaScript
- Converting numbers into corresponding alphabets and characters using JavaScript
- MySQL update datetime column values and add 10 years 3 months 22 days and 10 hours, 30 minutes to existing data?
- How can we create a MySQL function to find out the duration of years, months, days, hours, minutes and seconds?
- Converting array into increasing sequence in JavaScript
- Find the ratio of 3 years to 4 years 4 months.
- Why poles have days and nights of six months duration?

Advertisements