You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于动态输入计算时间差值并生成目标时间?附现有代码

Solution for Time Subtraction Calculation

Got it, let's work through this time calculation problem together. The core trick here is to convert everything to total minutes first—this avoids the mess of trying to subtract hours/minutes directly in string format. Here's a step-by-step implementation that'll handle your example perfectly:

Step 1: Convert the original time to total minutes

Take the form_t value (like "10:00"), split it into hours and minutes, then calculate how many minutes that is since midnight. This makes arithmetic way easier.

Step 2: Calculate total time to subtract

Add your fixed total_time and the user's dynamic travel_h input, then convert that sum to minutes (since we're working in minutes for consistency).

Step 3: Compute the new time in minutes

Subtract the total minutes to subtract from the original time's total minutes. If the result is negative (meaning we're wrapping back to the previous day), add 24*60 minutes (a full day) to fix it.

Step 4: Convert back to HH:MM format

Take the new total minutes, split into hours and minutes, then format them to two digits (so 2 becomes "02" and 30 stays "30").

Here's the full updated code with all these steps:

<input type="hidden" id="from_t" value="<?= date('H:i',strtotime($row['from_t'])) ?>">
<!-- Added step="0.5" to let users easily input half-hour values -->
<input style="width: 50px" type="number" id="h" min="0" step="0.5" class="trvael">

<script>
$(document).on('change','.trvael',function () {
    // Grab the original time string (e.g., "10:00")
    const originalTimeStr = $('#from_t').val();
    // Split into hours and minutes, convert to numbers
    const [originalHours, originalMinutes] = originalTimeStr.split(':').map(Number);
    // Calculate total minutes since midnight
    const originalTotalMinutes = originalHours * 60 + originalMinutes;

    // Define time values to subtract
    const fixedTotalTime = 5; // Fixed 5 hours
    const userTravelHours = parseFloat($('#h').val()) || 0; // Fallback to 0 if input is empty
    // Convert total subtract time to minutes
    const totalSubtractMinutes = (fixedTotalTime + userTravelHours) * 60;

    // Compute new total minutes (handle negative values for previous day)
    let newTotalMinutes = originalTotalMinutes - totalSubtractMinutes;
    if (newTotalMinutes < 0) {
        newTotalMinutes += 24 * 60; // Add a full day's worth of minutes
    }

    // Convert back to HH:MM format with leading zeros
    const newHours = Math.floor(newTotalMinutes / 60);
    const newMinutes = newTotalMinutes % 60;
    const formattedNewTime = `${newHours.toString().padStart(2, '0')}:${newMinutes.toString().padStart(2, '0')}`;

    // Use the result however you need—log it, update an element, etc.
    console.log(formattedNewTime); // Will output "02:30" for your example
});
</script>

Quick notes:

  • Added step="0.5" to the input so users can easily enter half-hour values like 2.5.
  • Added || 0 to userTravelHours to handle cases where the input is empty (avoids NaN errors).
  • padStart(2, '0') ensures hours and minutes are always two digits (so 9 becomes "09" instead of "9").

内容的提问来源于stack exchange,提问作者dror shalit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:26:51