# Calculating a daily figure

Hi everyone,

I’m looking for some help in calculating a ‘settlement figure’ depending on the day the user agrees to pay the loan back - for example if you choose todays date the figure could be £20,000. If you choose tomorrows it could be £20,005 and 2 days in the future could be £20,010. In this scenario, £5 per day is added… is there a smarter, more automated way of doing that rather that ‘if date = todays date then settlement figure is XXX’

Hopefully I’ve explained that well - thank you!

Yes, there are “smart” ways to do this using algorithms, but “date math” can be tricky. For example, 2 days in the future could be in the current month or the next month, and the current month might have 31 or 30 …or 29, or 28 days in it. …And realistically you might have to deal with business days not including weekends or holidays, day changes in different timezones, etc. And then Axure doesn’t support RegEx or full toLocaleString() options which would calculations and make currency formatting much easier (for a programmer.)

Here is a good demonstration from long ago on how you can calculate the number of days from a start date to an end date:

If you need to get the date 2 days in the future, you can use the .addDays(N) function like so:
`[[Now.addDays(2)]]`

Here is a demonstration that lets you input a loan rate (defaulted to your example of adding \$5 a day to 20,000, which would be a .025% rate), a loan amount, and a date. Clicking the Calculate button applies a mathematical expression using the values in the three input fields. The expressions are long and hard to read …also hard to create and debug–thanks, Axure!

Calculate rate by date offset.rp (62.5 KB)

Here is the main calculation:
`[[ ((((Date.parse(D) - Date.parse(Now.toLocaleDateString()))) / 86400000) * (R * (A.replace(",","")) / 100)) + A.replace(",","")) ]]`

• Date.parse() is a function that gives you the number of milliseconds since “the dawn of computers” (set at midnight, January 1, 1970). It is a way to get a pure number from any properly formatted date or date string with time. This enables you to do math with a date and/or time.
• D is a local variable pointing to the text of the date input field.
• The current date is subtracted from the input date and divided by 86400000 (the number of milliseconds in a day) to get the number of days between now and the future (input) date.
• R is a local variable pointing to the text of the rate input field.
• A is a local variable pointing to the text of the amount input field.
• A.replace(“,”,“”) gets rid of the formatted commas (otherwise the math won’t work)
• The rate is multiplied by the amount, and that subtotal is multiplied by the number of offset days.
• Finally, the resulting subtotal from all that is added to the original amount.

…and then I added some formatting:
`£ [[ (Target.text * 1).toLocaleString("en-GB") ]]`

• So the pound sign is included.
• .toLocaleString(“en-GB”) is a function to format strings. When it is a number (forced here with the otherwise silly `* 1` (multiply by one to make sure the string is evaluated as a number)), commas or dots are added, depending on the location of the browser. “en-GB” is the code for English language - Great Britain location.