从指定的日期/时间减去指定的小时数以获得新的日期/时间

问题描述:

我想知道如何通过从给定的到期日期/时间中减去给定的小时数来计算日期/时间,以便我知道什么时候需要在产品组件上启动一个流程步骤,以便及时完成向客户发货。从指定的日期/时间减去指定的小时数以获得新的日期/时间

例如,产品要准备就绪15:00出货给客户2017年9月15日

总装和验证步骤,从后到前,是

  • 步骤15 - 需要12.6小时完成
  • 步骤10 - 需要32.1小时完成
  • 步骤05 - 需要25.9小时完成

向后

  • 第15步的工作需要由15:00 2017年9月15日是完整运送到客户
  • 第10步需要由02:24在9/15是完整/ 2017准时开始第15步
  • 第05步需要在2017年9月13日06:18完成才能按时开始第10步
  • 所有组件都需要在04:24准备好装配在2017年9月12日准时开始第05步

我花了一整天的时间搜索一个足够接近我和我认为我将必须使用DateTime函数做什么的示例,我只是不确定目前是什么

+0

是步骤总是串行,还是可以运行一些parrallel?我们是否应该总是假设24小时的工作日。似乎是从完成日期时间中减去三次的简单总和:'= A1 - (SUM(B1:B3)/ 24)'其中A1是出​​货日期和时间,B1:B3是时间的位置它需要每一步。您可以修改公式来完成每一行。 –

+0

@ScottCraner - 在某些情况下,可以并行运行一些流程(尽管这将是基于机器可用性的车间决策)。在大多数情况下,工作日为24小时,但有些情况下(即库存),工作日可能因换班等而缩短 –

您可以从日期减去n小时,只需从中减去n/24。您还可以使用TimeSerial函数来减去小时,分钟和秒。这个例子说明了两种方法,它使用两种方法从当前时间减去1小时半。

Sub substractDates() 
    Dim d1 As Date, d2 As Date, d3 as Date 
    d1 = Now() 
    d2 = d1 - TimeSerial(1, 30, 0) 
    d3 = d1 - 1.5/24 
    Debug.Print d1, d2, d3 
End Sub 

p.s.第三种方法是使用TimeValue("1:30:0"),相当于TimeSerial(1, 30, 0)

这个宏会给你一个想法如何设置它:它可以进一步处理使用用户输入或从列表中花费时间。

Sub test() 
Dim dtmstart As Date 
Dim steps(2) As Double 
Dim duration, hours As Double 
dtmstart = "2017/9/15 15:00" 
steps(0) = 12.6 
steps(1) = 32.1 
steps(2) = 25.9 

MsgBox "Last step will need to be completed by: " & dtmstart 
For Each c In steps 
'convert hours to days 
hours = c/24 
duration = dtmstart - hours 
dtmstart = Application.Text(duration, "yyyy-mm-dd hh:mm") 
MsgBox dtmstart 
Next c 
End Sub 

DateAdd方法用于添加或减去Date对象。第一个参数是间隔类型。在下面的代码中,“h”表示小时,“n”表示分钟。要反向工作,请将您的小时和分钟输入为负值。

Dim dtShipTime As Date 
Dim dtStep15Start As Date 
Dim dtStep10Start As Date 
Dim dtStep05Start As Date 

dtShipTime = #9/15/2017 3:00:00 PM# 

dtStep15Start = DateAdd("h", -12, dtShipTime) 
dtStep15Start = DateAdd("n", -36, dtStep15Start) 

dtStep10Start = DateAdd("h", -32, dtStep15Start) 
dtStep10Start = DateAdd("n", -6, dtStep10Start) 

dtStep05Start = DateAdd("h", -25, dtStep10Start) 
dtStep05Start = DateAdd("n", -54, dtStep05Start) 

参考 http://www.chennaiiq.com/developers/reference/visual_basic/functions/dateadd.asp