跳到主要内容

计算两个日期相隔几天几小时几分钟

在搭建宜搭应用的过程中,你可能会遇到以下这些问题。

  • 薪酬管理系统中,通常需要根据员工的考勤时间计算并汇总员工的工作时间,来决定工资、加班费用或休假时间。
  • 生产管理系统中,通常需要计算生产周期,来决定后续如何安排生产和估算交货或发货时间。
  • 任务管理系统中,通常需要计算任务的完成时间核算人效,来决定后续如何安排人力。

前提条件

本教程使用到宜搭的表单公式,你可以先了解以下表单公式

实现效果

实现步骤

创建普遍表单页面

创建普通表单页面,详情请参考普通表单

在画布区域拖入以下组件。

  • 日期:命名为开始日期
  • 日期:命名为结束日期
  • 数值:命名为间隔毫秒数
  • 单行文本:命名为间隔时间

添加表单公式

开始日期选择快捷配置,默认今天。

结束日期选择快捷配置,指定为开始日期后 7 天。

间隔毫秒数选择公式编辑,并配置下图所示公式,也可以直接复制公式源码,注意修改字段。

{"text":"IF(OR(ISEMPTY(​开始日期​),ISEMPTY(​结束日期​)),0,​结束日期​-​开始日期​)","marks":[{"from":{"line":0,"ch":14,"sticky":null},"to":{"line":0,"ch":20,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":30,"sticky":null},"to":{"line":0,"ch":36,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":41,"sticky":null},"to":{"line":0,"ch":47,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":48,"sticky":null},"to":{"line":0,"ch":54,"sticky":null},"value":"dateField_ladso6iv","invalid":false}],"isCmData":true}

间隔时间选择公式编辑,并配置下图所示公式,也可以直接复制公式源码,注意修改字段。

{"text":"IF(OR(ISEMPTY(​开始日期​),ISEMPTY(​结束日期​)),\"0天0小时0分钟0秒\",CONCATENATE(TEXT(INT((​结束日期​-​开始日期​)/86400000),\"#天\"),TEXT(INT(((​结束日期​-​开始日期​)-INT((​结束日期​-​开始日期​)/86400000)*86400000)/3600000),\"#小时\"),TEXT(INT(((​结束日期​-​开始日期​)-INT((​结束日期​-​开始日期​)/3600000)*3600000)/60000),\"#分钟\"),TEXT(INT(((​结束日期​-​开始日期​)-INT((​结束日期​-​开始日期​)/60000)*60000)/1000),\"#秒\")))","marks":[{"from":{"line":0,"ch":14,"sticky":null},"to":{"line":0,"ch":20,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":30,"sticky":null},"to":{"line":0,"ch":36,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":74,"sticky":null},"to":{"line":0,"ch":80,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":81,"sticky":null},"to":{"line":0,"ch":87,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":116,"sticky":null},"to":{"line":0,"ch":122,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":123,"sticky":null},"to":{"line":0,"ch":129,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":136,"sticky":null},"to":{"line":0,"ch":142,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":143,"sticky":null},"to":{"line":0,"ch":149,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":198,"sticky":null},"to":{"line":0,"ch":204,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":205,"sticky":null},"to":{"line":0,"ch":211,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":218,"sticky":null},"to":{"line":0,"ch":224,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":225,"sticky":null},"to":{"line":0,"ch":231,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":276,"sticky":null},"to":{"line":0,"ch":282,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":283,"sticky":null},"to":{"line":0,"ch":289,"sticky":null},"value":"dateField_ladso6iv","invalid":false},{"from":{"line":0,"ch":296,"sticky":null},"to":{"line":0,"ch":302,"sticky":null},"value":"dateField_ladso6iw","invalid":false},{"from":{"line":0,"ch":303,"sticky":null},"to":{"line":0,"ch":309,"sticky":null},"value":"dateField_ladso6iv","invalid":false}],"isCmData":true}

保存页面

视频教程

在线试玩

Copyright © 2024钉钉(中国)信息技术有限公司和/或其关联公司浙ICP备18037475号-4