0

The following code is used to calculate the duration between two dates, for example "11/13/2012 11:21:41 AM" and "11/14/2012 2:32:59 PM". The function is producing the correct output, in this case: 1:03:11 (in dd:hh:mm format, so 1 day, 3 hours, and 11 minutes). After calculation the script stores this value into a Cell, which up until now is perfect, and exactly the format I want. After that, another script runs and moves that cells value depending on the needs and some logic in the script, and stores it to the required cell dynamically. The problem I'm having is, the duration is getting "AM" or "PM" added to the value, which is incorrect, since this is a very specific (and FORMATTED) DURATION, and NOT an actual time of day, which Excel seems to be treating as a TIME value instead.

Here is the code used to measure the durations:

Function TimeSpan(dt1,dt2)
Dim dtTemp

objExcel1.Application.ScreenUpdating = False
    If (IsDate(dt1) And IsDate(dt2)) = False Then
        TimeSpan = "00:00:00"
        Exit Function
    End If

    If dt2 < dt1 Then
        dtTemp = dt2
        dt2 = dt1
        dt1 = dt2
    End If
            TimeSpan = objExcel1.Application.WorksheetFunction.Text((dt2 - dt1), "dd:hh:mm")'"dd:hh:mm:ss"

objExcel1.Application.ScreenUpdating = False
End 
4

1 回答 1

0

查看FormatDateTime()Excel 中的函数。这可能会帮助您仅在单元​​格中存储您想要的内容。其次,当我使用 Excel 进行大量日期格式化时,我确保单元格包含我希望放入单元格中的数据类型的 FORMAT 信息。这可能会对您产生影响。在我看来,Excel 将您的值视为 TIME 值,可能是在“常规”单元格格式规则下。设置您将这些数据放入的单元格,以使用特定的“STRING”格式。这样,Excel 就不会对您的数据做出任何假设,然后您可以以任何您认为合适的方式解析它。

于 2014-11-07T23:30:25.410 回答