cumulative script calculation of depodit - javascript

A calculation that** returns 10% **of the deposit and adds the same 10% of the of the deposit every 24hr on the initial 10% after the deposit for 30 days , i obtained the 10% but i failed to add 10% on the initial 10%
<script>
new_deposit = (deposit * 0.1)
function calculate() {
// Get the current deposit amount from the form
var deposit = document.getElementById("deposit").value;
// Calculate the 10% increase
var new_deposit = (deposit * 0.1);
// Update the display of the deposit amount
document.getElementById("deposit_display").innerHTML = new_deposit;
}
// Run the calculate function every 24 hours (86400000 milliseconds)
setInterval(calculate, 86400000);
</script>

use code:
function calculate(money) {
money += money * 0.1
document.getElementById("deposit_display").innerHTML = money;
setTimeout(() => calculate(money), 86400000);
}
setTimeout(() => calculate(document.getElementById("deposit").value), 86400000);

Related

few doubts on following on Excel serial to date conversion code

I have a few doubts about following the code. can anyone please help me to understand the below code?
async function getNewDate(xlSerial) {
// milliseconds since 1899-31-12T00:00:00Z, corresponds to xl serial 0.
let xlSerialOffset = -2209075200000;
let elapsedDays;
// each serial up to 60 corresponds to a valid calendar date.
// serial 60 is 1900-02-29. This date does not exist on the calendar.
// we choose to interpret serial 60 (as well as 61) both as 1900-03-01
// so, if the serial is 61 or over, we have to subtract 1.
if (xlSerial < 61) {
elapsedDays = xlSerial;
}
else {
elapsedDays = xlSerial - 1;
}
// javascript dates ignore leap seconds
// each day corresponds to a fixed number of milliseconds:
// 24 hrs * 60 mins * 60 s * 1000 ms
let millisPerDay = 86400000;
let jsTimestamp = xlSerialOffset + elapsedDays * millisPerDay;
return new Date(jsTimestamp);
}
Here in the code can anyone tell me what is let xlSerialOffset = -2209075200000;
2)why do subtract 1 in second condition conditions?

Why is this function doesn't returns a time for the range specified?

Consider following code,
// Code from https://stackoverflow.com/a/66035934/14659574
const getRandomTime = (offsetHour = 0, rangeInHours = 2) => {
const curTicks = Date.now() + 0 * (offsetHour*60*60*1000);
const addTicks = (Math.random()*rangeInHours)*60*60*1000;
return new Date(curTicks + addTicks);
};
console.log((new Date()));
for(let i=0; i<10;i++) {
console.log( getRandomTime(1, 2+i));
}
It doesn't respect the range instead it returns random time exceeding the range. I want a random time within a range.
For example, Let's say I want a random time that's 2 hours from now but this function gives me values like 5 hours from now.
The code is taken from my previous question. It's looks like the author of the answer is inactive so I decided to make a follow up question.
I have dyscalculia and unable to debug this. I even tried to label the numbers but it not going anywhere.
What's the wrong with this code?
I believe the bug is in this part: Date.now() + 0 * (offsetHour * 60 * 60 * 1000);.
By multiplying the offset hour by zero, you are effectively resetting it to zero and negating the offset.
As a result, it's not possible to change the starting point from UTC.
With 0 * removed from the method, it works as intended:
const getRandomTime = (offsetHour = 0, rangeInHours = 2) => {
// Removed the offending 0 *
const curTicks = Date.now() + (offsetHour * 60 * 60 * 1000);
const addTicks = (Math.random() * rangeInHours) * 60 * 60 * 1000;
return new Date(curTicks + addTicks);
};
// Prints a time between UTC-5 and 1 hour from then.
console.log("Up to one hour from now: ",getRandomTime(-5, 1));
// Prints a time between UTC-5 and 2 hours from then.
console.log("Up to two hours from now: ",getRandomTime(-5, 2));
// Example continues
console.log("Up to three hours from now: ",getRandomTime(-5, 3));
console.log("Up to four hours from now: ",getRandomTime(-5, 4));

How to increase or decrease in percentage a time in hh:mm:ss format in javascript

I have a problem with javascript. I'm pretty new, so I appreciate your help in advance.
I have a form in which I have a date in hh: mm: ss format and to which through an input field I would like to increase or decrease the hh: mm: ss by percentage. The percentage can be integer or decimal number.
That is, in one hour "example".
17:22:14 I would like to increase by 6% or if it is 14.3% decimal, or else the same value but in negative, i. e. decrease by 6% or 14.3%.
I have an example to show the percentage of an increase from a given hour but I can't do what I want to do.
With this example in PHP I would know the percentage from a date, but I don't want this, I want to know how to increase or decrease in percentage from a given time and in javascript.
<?php
$date = '08:53:34';
$parts = explode(':', $date);
$secs = $parts[0] * 3600 + $parts[1] * 60 + $parts[2];
// day has 86 400 seconds
echo $secs / 864 . '%'; // return 37.05% (you can round it)
?>
And in javascript I have this example, but the other way around. What I want is to increase or decrease not knowing the increased time from a given hour.
var time_begin = '08:00:00';
var a = time_begin.split(':');
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var start_time = Math.round((seconds/(24*60*60))*100);
Thanks in advance
You can use for solve your problem standart JS object Date();
Algorithm is easy:
1) Make Date object of current or needed date. (today variable in my code)
2) Make Date of ending date, percentage to this date we'll looking
3) Just looking for difference between today date and tomorrow
4) Now we received percent that remained until tomorrow(Or other date).
5) If your need passed percents just use 100 - ((tomorrow - today) / ONE_DAY * 100) in last varibale, before use result
6) For increasing/decreasing use example.
Example is here:
const ONE_DAY = 8.64e7; // 86400000 milliseconds
let time = "08:54:33"; // Your time where your looking %
let today = new Date();
today.setHours(time.split(":")[0], time.split(":")[1], time.split(":")[2]); // Set time from what we will looking %
let tomorrow = new Date(Date.now() + ONE_DAY).setHours("0", "0", "0");
let percent = ((tomorrow - today) / ONE_DAY * 100).toFixed("2") + "%";
// it's for increase/decrease
const HOUR = 3.6e6; // 360000
const MINUTE = 6e4; // 60000
const SECOND = 1e3; // 1000
let incPercent = 0.03; // it's mean 3%
let increased = new Date(today.getTime() + (incPercent * HOUR));

Javascript/Moment - Issue with generating time slots

I am trying to generate time slots with a gap of 15min between each one, like the following :
["15:30", "15:45", "16:00", "16:15"...]
So far, I managed to make it. However, if the current time is 15:25 (just an example) the generated array will start from 15:30 what I need instead (in this case) to generate time slots starting from 16:00 meaning that only the first time slot should be approximately away 30 min from the current time.
Currently, I have the following code :
//Used momentJS library
function getTimeStops(end) {
var roundedUp, startTime, endTime, timeStops;
roundedUp = Math.ceil(moment().utc().minute() / 30) * 30;
startTime = moment().utc().set({
minute: roundedUp
});
endTime = moment(end, 'HH:mm');
if (endTime.isBefore(startTime)) {
endTime.add(1, 'day');
}
timeStops = [];
while (startTime <= endTime) {
timeStops.push(new moment(startTime).format('HH:mm'));
startTime.add(15, 'minutes');
}
return timeStops;
}
var timeStops = getTimeStops('02:00');
console.log('timeStops ', timeStops);
You're rounding to the nearest half hour here:
roundedUp = Math.ceil(moment().utc().minute() / 30) * 30;
Round to the nearest hour instead:
roundedUp = Math.ceil(moment().utc().minute() / 60) * 60;
Edit: just realised that the above is wrong and doesn't actually answer your question.
You do need to change your roundedUp value though.
What you need to do is:
Add 30 minutes to the current time
Round it to the closest 15 minute interval
That way - at most - you'll be 7.5 minutes out.
So for step 1, add 30 minutes
var add30mins = moment.utc().add(30, 'minutes')
Now we need to find the closest 15 minute interval.
var roundTo15Mins = Math.round(add30Mins.minute() / 15) * 15;
Then you can plug that into your startTime.
HTH

js count 15 min interval (double)

In JS i've a var {double} hour, where i want to know how many 15 minutes are in there.
// Example
var hour = 2.30
// Example calculation
// 1 hour = 60*60 = 3600
// 2 hour = 60*120 = 7200
// 15 min = 60 * 15 = 900
// 30 min = 60 * 30 = 1800
// count 15min within hour
var result = 7200 + 1800 = 9000 / 900 = 10
I can count the hours like this
var hour = 2.30
var tmp_hour = parseInt((hour.toString().split('.')[0]) * 60 * 60);
// tmp_hour = 7200
But i can't find a way to calculate the fractioanal part to minutes, see code
// this only working with::
// hour = 2.15, 2.45
// hour 2.15 => result => 900 (correct)
// hour 2.45 => result => 2700 (correct)
// hour 2.30 => result => 180 (not correct)
var tmp_min = parseInt(hour.toString().split('.')[1] / 15 * 900);
Does someone nows a better way for calculating this.
And i've another question whats could be offtopic.
I have a table with a couple of cells. When i click on a cell its get divided into four pieces (time interval, where every piece stands for 15 min).
// example
<table>
<tr>
<td></td>
<td></td>
<td>
<div class="divider">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</td>
<td></td>
</tr>
</table>
This is working fine, but i want to calculate the width for every label (4 pieces).
For instance if the clicked cell has a width of 17 px.
I want to following width
// span 1: 4px
// span 2: 5px
// span 3: 4px
// span 4: 4px
// total: 17px
I've tried several calculation with mod and dividing but could not get the disired result.

Categories