Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding the date at which money deposited equals a specific amount in JavaScript
When calculating compound interest over time, you may need to determine the exact date when your deposited money will reach a target amount. This problem involves daily compounding at a given annual interest rate.
Problem Statement
Given an initial deposit amount, a target amount, and an annual interest rate, we need to find the date when the deposited money (with daily compounding) will equal or exceed the target amount. The calculation starts from January 1st, 2021, with interest compounded daily at a rate of p percent divided by 360 days.
Solution Approach
The solution uses a while loop to simulate daily interest compounding until the current amount reaches the target. We track the number of days passed and add them to the starting date.
Example
Here's the complete implementation:
const principal = 100;
const amount = 150;
const interest = 2;
const findDate = (principal, amount, interest) => {
const startingDate = new Date('2021-01-01');
const dailyInterestRate = interest / 36000;
let currentMoney = principal;
let daysPassed = 0;
while (currentMoney < amount) {
daysPassed++;
currentMoney += currentMoney * dailyInterestRate;
}
startingDate.setDate(startingDate.getDate() + daysPassed);
return startingDate.toISOString().split('T')[0];
};
console.log(findDate(principal, amount, interest));
console.log(`It takes ${Math.ceil(Math.log(amount/principal) / Math.log(1 + interest/36000))} days to reach the target`);
Output
2040-12-26 It takes 14785 days to reach the target
How It Works
The algorithm follows these steps:
- Daily Interest Rate: Convert annual percentage to daily rate by dividing by 36000 (360 days × 100 for percentage)
- Compound Daily: Each day, add interest calculated on the current amount
- Track Days: Count days until the amount reaches or exceeds the target
- Calculate Date: Add the elapsed days to the starting date
Key Points
- The formula uses 360 days per year (banking convention) rather than 365
- Interest compounds daily, so each day's interest is calculated on the new total
- The function returns the date in YYYY-MM-DD format
- For the example (100 ? 150 at 2% annual), it takes approximately 40 years
Conclusion
This solution effectively calculates compound interest growth over time to determine when a deposit will reach a target amount. The daily compounding simulation provides accurate date prediction for financial planning scenarios.
