跳至主要内容

Kutools for Office — 一套工具,五种功能。事半功倍。

如何在Word文档中计算两个日期或时间之间的天数或小时数?

Author: Xiaoyang Last Modified: 2025-08-22

通常,我们可以快速轻松地在Excel工作表中计算两个日期之间的天数,但是,您是否尝试过在Word文档中获取两个给定日期之间的天数呢?

使用VBA代码计算Word文档中两个日期之间的差异

使用VBA代码计算Word文档中两个时间之间的差异


使用VBA代码计算Word文档中两个日期之间的差异

要计算两个给定日期之间的天数,以下VBA代码可以帮到您,请按照以下步骤操作:

1. 按住 ALT + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2. 然后,点击 插入 > 模块,将以下代码复制并粘贴到打开的空白模块中:

VBA代码:计算两个日期之间的差异

Sub CalculateDateDifference()
    Dim xStartDate As Date
    Dim xEndDate As Date
    Dim xDay As Long
    On Error Resume Next
    xStartDate = InputBox("Enter the start date", "KuTools for Word", "")
    xEndDate = InputBox("Enter the end date", "KuTools for Word", "")
   If (InStr(1, Str(xStartDate), ":") > 0) Or (InStr(1, Str(xEndDate), ":") > 0) Then
        MsgBox "please input current date", vbInformation, "KuTools for Excel"
        Exit Sub
    End If
    xDay = DateDiff("d", xStartDate, xEndDate)
    MsgBox "There are " & xDay & " days left from " & xStartDate & " to " & xEndDate & vbCrLf, vbInformation, "KuTools for Word"
End Sub

3. 然后,按 F5 键运行此代码,在连续弹出的对话框中,输入您要使用的开始日期和结束日期,参见截图:

Enter the start and end date

4. 然后,点击 确定 按钮,您将得到想要的结果,参见截图:

Pop-up displaying days between the two dates


使用VBA代码计算Word文档中两个时间之间的差异

这里还有另一个VBA代码可以帮助您计算两个给定时间之间的差异,请按照以下步骤操作:

1. 按住 ALT + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2. 然后,点击 插入 > 模块,将以下代码复制并粘贴到打开的空白模块中:

VBA代码:计算两个时间之间的差异

Sub CalculateTimeDifference()
    Dim xStartDate As Date
    Dim xEndDate As Date
    Dim xTime As Long
    Dim xHour As Long
    On Error Resume Next
    xStartDate = InputBox("Enter the start time", "KuTools for Word", "")
    xEndDate = InputBox("Enter the end time", "KuTools for Word", "")
    Debug.Print Str(xStartDate)
    If (Str(xStartDate) = " 0:00:00") Or (Str(xEndDate) = " 0:00:00") _
    Or (Str(xStartDate) = " 12:00:00 AM") Or (Str(xEndDate) = " 12:00:00 AM") Then
        MsgBox "please input the time", vbInformation, "KuTools for Excel"
        Exit Sub
    ElseIf xStartDate > xEndDate Then
        MsgBox " The start time is not larger than the end time!", vbInformation, "KuTools for Excel"
        Exit Sub
    End If
    xTime = DateDiff("s", xStartDate, xEndDate)
    xHour = xTime \ 3600
    xTime = xTime - xHour * 3600
    MsgBox "There are " & xHour & " hours " & xTime \ 60 & " minutes " & xTime - (xTime \ 60) * 60 _
            & " seconds left from " & xStartDate & " to " & xEndDate & vbCrLf, vbInformation, "KuTools for Word"
End Sub

3. 然后,按 F5 键运行此代码,在连续弹出的对话框中,输入您要用于计算差异的开始时间和结束时间,参见截图:

Enter the start and end time

4. 然后,点击 确定 按钮,两个给定时间之间的时间差已计算并显示如下截图所示:

Pop-up displaying time difference between the two times

最佳Office办公效率工具

Kutools for Word - 用超过100项强大功能全面提升你的Word体验!

🤖 Kutools AI 功能AI助手 / 实时助手 / 超级润色(保留格式)/ 超级翻译(保留格式)/ AI遮挡 / AI校正……

📘 文档掌控分割页面 / 文档合并 / 区域导出为多种格式(PDF/TXT/DOC/HTML等)/ 批量转换为PDF……

正文编辑跨多个文件批量查找与替换 / 一键调整所有图片大小 / 翻转表格行列 / 表格转文本……

🧹 高效清理:一扫多余空格 / 分节符 / 文本框 / 超链接 / 想了解更多删除工具,请前往删除分组……

创意插入:插入千位区分符 /复选框 /选项按钮 / 二维码 / 条形码 / 批量插入图片 / 更多内容请见插入分组……

🔍 精准选取:快速定位指定页面 / 表格 / 形状 / 标题段落 / 利用更多 选择 功能提升导航体验……

实用增强跳转任意区域 / 自动插入重复文本 / 文档窗口间一键切换 /11 转换工具……

🌍 支持40多种语言:自由选择你喜欢的界面语言——Kutools支持英语、西班牙语、德语、法语、中文及其他40余种语言!

Kutools and Kutools Plus tabs on the Word Ribbon
👉 想立即体验这些功能?马上下载 Kutools for Word!🚀
 

✨ Kutools for Office – 一次安装,五大强力工具集成!

内含 Office Tab Pro · Kutools for Excel · Kutools for Outlook · Kutools for Word · Kutools for PowerPoint

📦 五大工具一站式集成 | 🔗 无缝兼容Microsoft Office | ⚡立即提升效率、节省时间

最佳Office办公效率工具

Kutools for Word - 100+ 项Word工具