0

我有两个 radtimepicker,我想创建一个 javascript 来计算它们中的两个以产生总分钟数。我的场景是我想计算用户选择的两个小时的总分钟数:

例子:

radtimepicker1: 09:00 AM
radtimepicker2: 10:15 AM

Total minutes: 75

这是我的代码,但没有运气:

  function CalculateTotalMinutes(sender, eventArgs) {


        var timeFrom= $find("<%= timepickerFrom.ClientID %>");
        var varFrom= timeFrom.get_timeView();

        var timeTo= $find("<%= timepickerTo.ClientID %>");
        var varTo = timeTo.get_timeView();

        var txtNumDays = $find("<%= txtDuration.ClientID%>");

        if (varFrom <= varTo ) {


            var varSeconds = Math.ceil((varTo.getTime() - varFrom.getTime()) / 1000);
            var varMinutes = varSeconds / 60;
            txtNumDays.set_value(varMinutes);
        }
        else {

        }

    }

请帮助...提前谢谢...

4

2 回答 2

0

请尝试使用以下代码片段。

JS

<script type="text/javascript">
        function DateSelected(sender, args) {
            var timeFrom = $find("<%= RadTimePicker1.ClientID %>");
            var varFrom = timeFrom.get_selectedDate();

            var timeTo = $find("<%= RadTimePicker2.ClientID %>");
            var varTo = timeTo.get_selectedDate();

            if (varFrom != null && varTo != null) {
                alert(daysBetween(varFrom, varTo));
            }
        }

        function daysBetween(date1, date2) {
            //Get 1 day in milliseconds
            var one_day = 1000 * 60 * 60 * 24;

            // Convert both dates to milliseconds
            var date1_ms = date1.getTime();
            var date2_ms = date2.getTime();

            // Calculate the difference in milliseconds
            var difference_ms = date2_ms - date1_ms;
            //take out milliseconds
            difference_ms = difference_ms / 1000;
            var seconds = Math.floor(difference_ms % 60);
            difference_ms = difference_ms / 60;
            var minutes = Math.floor(difference_ms % 60);
            difference_ms = difference_ms / 60;
            var hours = Math.floor(difference_ms % 24);
            var days = Math.floor(difference_ms / 24);

            return days + ' days, ' + hours + ' hours, ' + minutes + ' minutes, and ' + seconds + ' seconds';
        }

    </script>

ASPX

<telerik:RadTimePicker ID="RadTimePicker1" runat="server">
        <ClientEvents OnDateSelected="DateSelected" />
</telerik:RadTimePicker>
<telerik:RadTimePicker ID="RadTimePicker2" runat="server">
        <ClientEvents OnDateSelected="DateSelected" />
</telerik:RadTimePicker>
于 2013-09-23T05:32:52.193 回答
0

我修改了您的代码以满足我的需要....非常感谢

function DateSelected(sender, args) {
        var timeFrom = $find("<%= RadTimePicker1.ClientID %>");
        var varFrom = timeFrom.get_selectedDate();

        var timeTo = $find("<%= RadTimePicker2.ClientID %>");
        var varTo = timeTo.get_selectedDate();

        if (varFrom != null && varTo != null) {
            alert(daysBetween(varFrom, varTo));
        }
    }

    function daysBetween(date1, date2) {
        //Get 1 day in milliseconds
        var one_day = 1000 * 60 * 60 * 24;

        // Convert both dates to milliseconds
        var date1_ms = date1.getTime();
        var date2_ms = date2.getTime();

        var varSeconds = ((date2_ms - date1_ms) / 1000);

        var varMinutes = varSeconds / 60;

        return varMinutes + ' minutes';
    }
于 2013-09-23T07:40:14.503 回答