简体   繁体   English

价格计算-Javascript

[英]Price Calculation - Javascript

So, I am developing a web based application for an airport parking. 因此,我正在为机场停车开发基于Web的应用程序。 I am using Twitter bootstrap for the form. 我正在为表单使用Twitter引导程序。 To get the date and time range of every enquiry I am using Date Range Picker for Bootstrap. 为了获取每个查询的日期和时间范围,我使用Bootstrap的Date Range Picker。 What I did was to dynamically change a label's value called "Total Price" which is found in the booking page based on the Date Range Picker's inputs (located on the top). 我要做的是根据日期范围选择器的输入(位于顶部)动态更改标签值“总价”,该值可在预订页面中找到。 The charging rates are 0.01 per hour. 收费率为每小时0.01。 For example: In case I am in the process of my enquiry and I choose to drop my car off at Saturday 10 (00:00) and pick it up at Sunday 11 (00:00) the price label should display "24€". 例如:如果我正在查询中,并且选择在星期六10(00:00)放车并在星期日11(00:00)取车,则价格标签上应显示“ 24€” 。 I attach the code that calculates the price. 我附上了计算价格的代码。

$(document).ready(function () {   
var select=function(dateStr) {
    var d1 = $('#date1').datepicker('getDate');
    var d2 = $('#date2').datepicker('getDate');
    var diff = 0;
    var currentTime    = moment(d1);
    var endTimeJS      = moment(d2);
    var timeRemainJSQ  = endTimeJS.diff(currentTime,'hours')//To get the difference in hours
    //alert(timeRemainJSQ)
    diff    =  timeRemainJSQ/3.42857142857143; //multiply with 0.01 per hour
    $('#calculated').val(diff);
    $('#testid').html(diff);

}

$("#date1").datepicker({ 
    onSelect: select
});
$('#date2').datepicker({onSelect: select});
});

So far I achieved to calculate the price based on the date range and I am looking for a way to implement some price manipulation based on the drop-off and pick-up time. 到目前为止,我已经实现了根据日期范围来计算价格,并且我正在寻找一种基于下车和接送时间来实施价格操纵的方法。 I want my code to charge the drop-off day as an 24h full day only if the drop-off time exceeds 6 hours. 我希望我的代码仅在下车时间超过6小时时才将下车日作为24小时全天收取。 Same with the pick-up day. 接车日也一样。 For example, if I drop my car or motorcycle off at Tuesday November 10 at 18:00 the system should charge 6 hours (0.01 per hour). 例如,如果我在11月10日(星期二)18:00放下汽车或摩托车,则系统应收费6个小时(每小时0.01个小时)。 If however the drop-off time is at 17:00 it should charge a whole day. 但是,如果下车时间为17:00,则应整天收费。

Link to the working page . 链接到工作页面

It is my understanding that this might me too much to ask but I tried as far as I could with no result. 我的理解是,这可能让我提出太多要求,但我尽了最大努力,但没有结果。 Thank you in advance. 先感谢您。

Intro 介绍

In your code there was few issues, I ended up stop debugging and focused on your main question, which is calculation of fee. 在您的代码中几乎没有问题,我最终停止了调试,并专注于您的主要问题,即费用的计算。

I have re-done the part was required to answering your question and you need to spend a little bit time fixing/debugging your code and implement my solution in it. 我已经重新完成了回答您的问题所需的部分,您需要花费一些时间来修复/调试代码并在其中实现我的解决方案。

Note , in JavaScript it is important to keep the correct order of the code, one of the issues was jQuery conflicting. 注意 ,在JavaScript中,保持正确的代码顺序很重要,其中一个问题是jQuery冲突。 Any way try to copy my code and it works. 任何方式尝试复制我的代码,它都可以工作。

You might import the libraries if you intend to work offline. 如果打算脱机工作,则可以导入库。

Solution

HTML HTML

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="//jonthornton.github.io/jquery-timepicker/jquery.timepicker.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<script src="//jonthornton.github.io/jquery-timepicker/jquery.timepicker.js"></script>

<form method="post" action="makecleaningappt">
    From<br/>
    <input type="text" id="datepicker" name="date"><input type="text" id="timepicker1" name="time"><br/>
    To<br/>
    <input type="text" id="datepickerEnd" name="date"><input type="text" id="timepickerEnd1" name="time"><br/>
</form>

JavaScript with description to be placed after the HTML code 带有说明的JavaScript将放在HTML代码之后

<script type="application/javascript">
    $(document).ready(function () {

        var select = function () {
            priceCal();
        };

        $("#datepickerStart").datepicker({
            onSelect: select,
            onUpdate: select
        });
        $("#timepickerStart").timepicker({
            onSelect: select,
            onUpdate: select
        });
        $("#datepickerEnd").datepicker({
            onSelect: select,
            onUpdate: select
        });
        $("#timepickerEnd").timepicker({
            onSelect: select,
            onUpdate: select
        });
    });

    $(function () {
        $('#datepickerStart').datepicker({
            'format': 'm/d/yyyy',
            'autoclose': true
        });
    });

    $(function () {
        $('#timepickerStart').timepicker({
            'showDuration': true,
            'timeFormat': 'g:ia'
        });
    });

    $(function () {
        $('#datepickerEnd').datepicker({
            'format': 'm/d/yyyy',
            'autoclose': true
        });
    });

    $(function () {
        $('#timepickerEnd').timepicker({
            'showDuration': true,
            'timeFormat': 'g:ia'
        });
    });

    function priceCal() {
        //declares
        var hourRate = 0.01;
        var dayRate = 6;
        var fullDayHours = 6 * 60;

        var dateStart = $('#datepickerStart').datepicker('getDate');
        var hourStart = $('#timepickerStart').timepicker('getTime');
        var dateEnd = $('#datepickerEnd').datepicker('getDate');
        var hourEnd = $('#timepickerEnd').timepicker('getTime');

        var totalDays = (dateEnd - dateStart) / 24 / 60 / 60 / 1000; //we get total days
        var totalHours = (hourEnd - hourStart) / 60 / 1000;          //we get total minutes

        if (totalDays > 0) {
            //more than one day
            console.log("Parked for " + totalDays + " day/s it cost " + (totalDays * dayRate));
        } else {
            if (totalHours > 0) {
                //less then 1 day
                if (totalHours >= fullDayHours) {
                    // more than 6 hours
                    console.log("Parked for " + totalHours + " minutes it cost " + dayRate);
                } else {
                    // less than 6 hours
                    console.log("Parked for " + totalHours + " minutes it cost " + (totalHours * hourRate));
                }
            }
        }

    }
</script>

Input and output 输入输出

From 11/09/2015 to 11/08/2015 time 12:00am to 11:00am       no results
From 11/09/2015 to 11/09/2015 time 12:00am to 12:00am       no results
From 11/09/2015 to 11/09/2015 time 12:00am to 12:30am       30 minutes cost 0,3
From 11/09/2015 to 11/09/2015 time 12:00am to 06:00am       360 minutes cost full day 6
From 11/09/2015 to 11/10/2015 time any time                 1 day cost full day 6

Screenshot 截图

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM