0

I've searched for a while and couldn't find anyone even touching on this subject.

I have a price of $900 slowly approach $1500 between the dates of April 1st and August 1st. That's about $5 per day, and the formula would be:

ROUND(1500 - (600 * ((DEADLINE - TODAY) / (DEADLINE - START)))

But how do I express that in terms of javascript and HTML so that I can write it out in a sentence like "Today you'll have to pay $920."

This is where I'm at so far, but I can't figure out how to zero today's date to achieve the correct calculation:

<div id="foo"></div>


  <script type="text/javascript">

      function getDayDiff(a, b) {
        return (a - b) / 8.64e7;
    }

    function getPayAmount(DEADLINE, TODAY, START) {
        return Math.round(1500 - (600 * getDayDiff(DEADLINE, TODAY) / getDayDiff(DEADLINE, START)));
    }
    
 document.getElementById("foo").innerHTML = "Today you'll have to pay $"  + getPayAmount(new Date(2021, 08, 1), new Date(), new Date(2021, 4, 1));
    </script>  
  • All the answers here assume every day is 8.64e7 ms long, which isn't true where daylight saving is observed. see [*How do I get the number of days between two dates in JavaScript?*](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) They also mostly depend on the vagaries of the built–in parser, which is not recommended, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 03 '21 at 21:25

4 Answers4

1

First calculate days (since unix time), using getTime() (which will give milliseconds) and use the same formula.

const days = (date) => Math.ceil(date.getTime() / (24 * 60 * 60 * 1000));

const DEADLINE = days(new Date("2021-08-01"));
const START = days(new Date("2021-04-01"));
const TODAY = days(new Date());

const res = Math.round(1500 - 600 * ((DEADLINE - TODAY) / (DEADLINE - START)));

console.log(res);

document.querySelector('.adbox.adred').append(`Today's Rate ${res}`)
Siva K V
  • 10,561
  • 2
  • 16
  • 29
1

This is how you can do it in Javascript:

    function getDayDiff(a, b) {
        return (a - b) / 8.64e7;
    }

    function getPayAmount(DEADLINE, TODAY, START) {
        return Math.round(1500 - (600 * getDayDiff(DEADLINE, TODAY) / getDayDiff(DEADLINE, START)));
    }
    
    document.getElementById("foo").innerHTML = "Today you'll have to pay $"  + getPayAmount(new Date(2021, 12, 31), new Date(2021, 4, 3), new Date(2021, 1, 1));
<div id="foo"></div>

Explanation:

  • 8.64e7 is the number of milliseconds in a day
  • date differences are computed in milliseconds
  • ROUND is Math.round

EDIT

I have seen this try in the edited question:

<div id="foo"></div>


  <script type="text/javascript">

      function getDayDiff(a, b) {
        return (a - b) / 8.64e7;
    }

    function getPayAmount(DEADLINE, TODAY, START) {
        return Math.round(1500 - (600 * getDayDiff(DEADLINE, TODAY) / getDayDiff(DEADLINE, START)));
    }
    
 document.getElementById("foo").innerHTML = "Today you'll have to pay $"  + getPayAmount(new Date(2021, 08, 1), new Date(2021, 4, 6), new Date(2021, 4, 1));
    </script>  

One can create an object and zero the hours, like this:

 var today = new Date();
 today.setHours(0, 0, 0);
 document.getElementById("foo").innerHTML = "Today you'll have to pay $"  + getPayAmount(new Date(2021, 08, 1, 0, 0, 0), today, new Date(2021, 4, 1, 0, 0, 0));
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • There aren't always 8.64e7 ms in a day where daylight saving is observed, see [*How do I get the number of days between two dates in JavaScript?*](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – RobG Apr 03 '21 at 21:22
  • @RobG thanks for pointing that out. If DST happens twice in the interval, then the issue is automatically solved. If a single such day happens, then the problem may exist. In such cases that can be added to the calculation. Nevertheless, I think this is a good starting point that can be enhanced by cultural/political patterns being added. They would make this slightly more complicated. If the asker considers this to be a problem, then I will edit my answer accordingly. – Lajos Arpad Apr 04 '21 at 12:58
  • @LajosArpad—since it seems only full days need to be counted, DST issues are easily avoided by setting the dates to 00:00:00, then dividing the difference by 8.64e7 and rounding. The ± DST fraction (if it exists) will round out. :-) – RobG Apr 05 '21 at 11:47
0

This could certainly be cleaned up for efficiency, but I wanted to show you how it could work using each of the variables.

// To set two dates to two variables
let date1 = new Date("04/01/2020");
let date2 = new Date("08/01/2020");
  
// To calculate the time difference of two dates
let Difference_In_Time = date2.getTime() - date1.getTime();
  
// To calculate the no. of days between two dates
let Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

//Set your two price points
let price1 = 900;
let price2 = 1500;

//Calculate price delta
let Difference_In_Price = price2 - price1;

//Divide the price delta and the days delta. Setting the value to a two decimal place fixed value.
let Price_Over_Period = (Difference_In_Price / Difference_In_Days).toFixed(2);

document.write(Price_Over_Period);

You can see the code in action here: https://jsfiddle.net/ugzn2y7s/

zeropsi
  • 682
  • 9
  • 24
  • `//Trying to add this to see if I can finish it off to get how much they'd have to pay today. let Price_Today = (price1 + Price_Over_Period).toFixed(2); document.write(Price_Today);` – Jon Sanders Apr 03 '21 at 13:41
  • Your fiddle provides me with the price hike per day, but I'm looking for the new total for that given day. I tried to append, but to no avail. – Jon Sanders Apr 03 '21 at 14:24
0

I used the starting, end and today's dates to find the rate of the pay,

Then I added the rate to the starting amount to find how much should have been paid today

const today = new Date();
const startDate = new Date('April 1, 2021');
const endDate = new Date('August 1, 2021');
const deadline = (endDate - startDate) / (1000 * 60 * 60 * 24);
const daysDiff = (today - startDate) / (1000 * 60 * 60 * 24);

const rate = Math.round((1500 - 900) / deadline);

document.body.innerText = `You should have paid $${Math.round((daysDiff * rate) + 900)} by today`;
a.mola
  • 3,883
  • 7
  • 23
  • The snippet works, but I can't get the code to work elsewhere. Should I be referencing a library in the section or something? All I've done so far is add the `` tags around what you've provided, but still everything is blank. Thank you so much for your time. – Jon Sanders Apr 03 '21 at 14:09
  • Try logging out the variables to confirm the Javascript is being run. Or instead of changing the innerText of the document, try and alert it out. – a.mola Apr 03 '21 at 14:14