简体   繁体   English

如何从 2 个用户输入中获取天数,然后将其与另一个用户输入相乘,即 JavaScript 中的金额?

[英]How do I get the number of days from 2 user inputs and then multiply it with another user input which is amount in JavaScript?

I'm trying to create a simple fine calculator, I have two user input dates and a user input amount.我正在尝试创建一个简单的精细计算器,我有两个用户输入日期和一个用户输入数量。 I need to subtract the 2 dates and then multiply the number of days by the fine amount.我需要减去 2 个日期,然后将天数乘以罚款金额。 I went through a bunch of videos about dates, but none of them ever take user input.我浏览了一堆关于日期的视频,但没有一个接受过用户输入。 I am very new to javascript so I don't know why it won't show me my result.我是 javascript 的新手,所以我不知道为什么它不显示我的结果。 Could someone tell me what's wrong with my code?有人可以告诉我我的代码有什么问题吗?

 if (isset($_POST['calcDiff'])) { $("#bdate").datetimepicker({ timepicker: false, format: 'ymd' }); $("#rdate").datetimepicker({ timepicker: false, format: 'ymd' }); function calcDiff() { var bdate = new Date($("#bdate").val()); var rdate = new Date($("#rdate").val()); var amount = $('#amount').val(); var timeDifference = rdate.getTime() - bdate.getTime(); var milliSecondsInOneSecond = 1000; var secondInOneHour = 3600; var hourInOneDay = 24; var daysDiff = timeDifference / (milliSecondsInOneSecond * secondInOneHour * hourInOneDay); var fine = daysDiff * amount.val(); alert(fine); } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form name="finecalc" action="" method="post"> <p> Borrowed date <input type="date" name="bdate" id="bdate" required="required" value="<?php echo $bdate; ?>" /> </p> <p> Return date</b> <input type="date" name="rdate" id="rdate" required="required" value="<?php echo $rdate; ?>" /> <b> </p> <p>Enter fine amount per day</b> <input type="text" name="amount" size="10"><b> </p><button onclick="calcDiff()">Calculate Fine</button><p id="display"></p> </form>

var amount = $('#amount').val();
...
var fine = daysDiff * amount.val();

Maybe no need to use amount.val() ?也许不需要使用amount.val()

var fine = daysDiff * amount;

Also, remove the comma in this line:另外,删除此行中的逗号:

var daysDiff = timeDifference / (, milliSecondsInOneSecond * secondInOneHour * hourInOneDay);

First you need to calculate difference between 2 dates and total number of days首先,您需要计算 2 个日期与总天数之间的差异

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); // calculate number of days 
console.log(diffDays + " days");
var amount = $('#amount').val(); //store Amount value into amount variable

Then calculate fine: var fine = daysDiff * amount;然后计算罚款: var fine = daysDiff * amount;

You can get the value from each input, then actually just subtract the dates to get the epoch difference and with that number you can convert it to days and multiply by the fine.您可以从每个输入中获取值,然后实际上只需减去日期以获得纪元差异,并使用该数字将其转换为天数并乘以罚款。

I added a JS that does just that.我添加了一个 JS 来做到这一点。

 function calcDiff(){ const date1 = new Date(document.querySelector('#bdate').value); const date2 = new Date(document.querySelector('#rdate').value); const penalty = document.querySelector('input[name="amount"]').value; const diff_ms = date2 - date1; // ms -> s -> min -> h -> days const days = diff_ms / 1000 / 60 / 60 / 24; const amount_to_pay = days * penalty; console.log('$' + amount_to_pay); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form name="finecalc" action="" method="post"> <p> Borrowed date <input type="date" name="bdate" id="bdate" required="required" value="<?php echo $bdate; ?>" /> </p> <p> Return date</b> <input type="date" name="rdate" id="rdate" required="required" value="<?php echo $rdate; ?>" /> <b> </p> <p>Enter fine amount per day</b> <input type="text" name="amount" size="10"><b> </p><button type="button" onclick="calcDiff()">Calculate Fine</button><p id="display"></p> </form>

This is what wound up working for me, thank you to all that helped.这就是最终为我工作的原因,感谢所有帮助过的人。

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="enterbooks.css" />
</head>
<body style="background: url(library.png); background-size: 100%;">
    <div class="content">
        <h1>Hey admin calulate you fines here!</h1>
        <script>
            function calcDiff() {

                const date1 = new Date(document.querySelector('#bdate').value);
                const date2 = new Date(document.querySelector('#rdate').value);
                const penalty = document.querySelector('input[name="amount"]').value;
                const diff_ms = date2 - date1;
                // ms -> s -> min -> h -> days
                const days = diff_ms / 1000 / 60 / 60 / 24;
                const amount_to_pay = days * penalty;

                alert(amount_to_pay);

            }
        </script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

        <form name="finecalc" action="" method="post">
            <p>
                Borrowed date
                <input type="date" id="bdate" required="required" value="<?php echo $bdate; ?>" />
            </p>
            <p>
                Return date</b>
                <input type="date" id="rdate" required="required" value="<?php echo $rdate; ?>" /> <b>
            </p>
            <p>Enter fine amount per day</b>
                <input type="text" name="amount" size="10"><b>
            </p><button type="button" onclick="calcDiff()">Calculate Fine</button>
            <p id="display"></p>
        </form>
    </div>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 乘以 javascript 中用户的输入 - Multiply input from user in javascript 如何获取angularjs中的用户输入并将其从一种模式传递到另一种? 如何在javascript中连接控制器? - How can I get the user inputs in angularjs and pass it from one modal to another? How controllers are connected in javascript? 我如何使该Cookie从今天起14天后过期(无论何时用户生成) - how do i get this cookie to expire after 14 days from today(which will be whenever the user generates) 如何使用 Javascript 将来自不同输入的值相乘以获得总数? - How do I multiply values from different inputs to get a total using Javascript? 如何获取用户选择的酒店房间并乘以住宿天数? - How to take user selected hotel room and multiply by number of days stayed? 我如何从下拉列表中获取项目作为输出两次或三次或任意次数作为用户通过 JavaScript 输入 - How can i get item from dropdown list as output twice or thrice or any number of times as user inputs through JavaScript 如何让此脚本将输入字段中输入的数字相乘? - How do I get this script to multiply a number that was typed into the input field? JavaScript:如何将包含逗号的输入从用户转换为数字? - JavaScript: How do I convert input that contains commas from a user to a number? 如何使用 javascript 在另一个网页上使用 document.getElementById() 显示用户输入? - How do I display user input using document.getElementById() from one webpage on another using javascript? Javascript:如何编写一个脚本,在该脚本中用户输入一个数字并返回其他三个数字? - Javascript: How do I code a script where the user inputs a number and it returns three other numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM