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

Blue Prism中文本日期时间计算与格式适配问题咨询

解决方案:处理日/月/年格式的文本日期并计算请假天数

Hey there! Let's work through this date calculation problem together. The core issue here is that you can't directly subtract text-based dates—we first need to convert your day/month/year formatted strings into actual date values that Excel or VBA can recognize and compute with. Here are two reliable approaches:

方法1:Excel公式(无需编程)

For a date text like 11/3/2017 12:00:00 AM in cell A1, use this formula to convert it to a standard date value:

=DATE(
    RIGHT(LEFT(A1,FIND(" ",A1)-1),4),  // Extract the 4-digit year
    MID(LEFT(A1,FIND(" ",A1)-1),FIND("/",A1)+1,FIND("/",A1,FIND("/",A1)+1)-FIND("/",A1)-1),  // Extract month
    LEFT(LEFT(A1,FIND(" ",A1)-1),FIND("/",A1)-1)  // Extract day
)

This formula automatically ignores the time portion and works regardless of whether day/month are 1 or 2 digits (e.g., 1/3/2017 or 11/3/2017).

To calculate the number of leave days between two dates (A1 = start, B1 = end), wrap the conversion in an absolute value to avoid negative numbers:

=ABS(
    DATE(RIGHT(LEFT(A1,FIND(" ",A1)-1),4),MID(LEFT(A1,FIND(" ",A1)-1),FIND("/",A1)+1,FIND("/",A1,FIND("/",A1)+1)-FIND("/",A1)-1),LEFT(LEFT(A1,FIND(" ",A1)-1),FIND("/",A1)-1))
    -
    DATE(RIGHT(LEFT(B1,FIND(" ",B1)-1),4),MID(LEFT(B1,FIND(" ",B1)-1),FIND("/",B1)+1,FIND("/",B1,FIND("/",B1)+1)-FIND("/",B1)-1),LEFT(LEFT(B1,FIND(" ",B1)-1),FIND("/",B1)-1))
)

方法2:VBA自定义函数(更简洁灵活)

If you need to do this frequently, a custom VBA function will save you time:

Function ConvertDMYToDate(dateText As String) As Date
    ' Split off the time portion to focus on the date
    Dim datePart As String
    datePart = Split(dateText, " ")(0)
    
    ' Split day, month, year using the slash separator
    Dim dateComponents() As String
    dateComponents = Split(datePart, "/")
    
    ' Convert to a proper date value (year, month, day order)
    ConvertDMYToDate = DateSerial(CInt(dateComponents(2)), CInt(dateComponents(1)), CInt(dateComponents(0)))
End Function

Once you add this to your VBA editor, use it in Excel like any other function:

=ABS(ConvertDMYToDate(A1) - ConvertDMYToDate(B1))

This handles variable-length date strings flawlessly since Split doesn't care about how many digits each component has.

Quick note on "removing irrelevant content"

If you just want to clean up the text (e.g., strip the time), use this formula to extract only the date part:

=LEFT(A1,FIND(" ",A1)-1)

But remember—this still gives you a string, not a computable date. You'll still need to convert it using one of the methods above to calculate days.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:47:48