为何DateDiff返回超大负数?VB脚本计时问题求助
Hey there! Let's break down why you're getting that wild negative number (-3733642190 seconds) when trying to time your operation, and how to fix it quickly.
Why the Negative Number Happens
That massive negative value is almost certainly due to incorrect time calculation logic—likely using date-based functions (like Now()) that rely on VBScript's internal date timestamp (which counts days since December 30, 1899). If your start/end time calculation crosses an unexpected boundary, or if you mixed up the order of start/end values in a way that triggers integer overflow, you'll end up with nonsensical negative results.
The Simple Fix: Use the Timer Function
VBScript has a built-in Timer function that's perfect for short-duration timing (like your 20-second target). It returns the number of seconds elapsed since midnight as a single-precision float, so it avoids date-related overflow issues entirely.
Here's how to rewrite your code with proper timing:
' Capture start time (seconds since midnight) Dim startTime startTime = Timer ' Execute your operation (complete your existing code here) PbWindow("w_mainframe").PbWindow("w_cash_detail").PbDataWindow("dw_search_main").SelectCell "#1","Contract_Party_Name" ' Add the rest of your page load/operation steps here... ' Capture end time Dim endTime endTime = Timer ' Calculate elapsed time (round to 2 decimal places for readability) Dim elapsedSeconds elapsedSeconds = Round(endTime - startTime, 2) ' Check if your target is met If elapsedSeconds < 20 Then MsgBox "Operation completed in " & elapsedSeconds & " seconds (meets target!)" Else MsgBox "Operation took " & elapsedSeconds & " seconds (exceeds 20-second target)" End If
Key Notes
- Always subtract
startTimefromendTime—reversing them will give a negative number, but usingTimerensures the value stays within a 0-86400 range (since there are 86400 seconds in a day). - Since your target is under 20 seconds,
Timeris ideal. For operations that might span multiple days, you'd combineDateDiffwithTimer, but that's unnecessary here.
内容的提问来源于stack exchange,提问作者JPWat

