简体   繁体   English

Excel VBA时间和日期

[英]Excel VBA Time and Date

I am trying to figure out how to alter this code so that it will work for what I need. 我试图弄清楚如何更改此代码,以便它可以满足我的需要。 I want to basically say that if today is Friday and the time is 4:00PM or greater, then the new time is Monday 7:00AM. 我想说的是,如果今天是星期五,时间是4:00 PM或更大,那么新时间是星期一7:00 AM。 Here is what I have but I cannot figure out the TIME part of it. 这是我所拥有的,但我无法弄清楚它的“时间”部分。

modDropOff = Now
LHour = Hour("4:00:00 PM")
LNewHour = Hour("07:00:00 AM")

If Weekday(Date) = vbFriday And Now() >= LHour Then
modDropOff = Date + 3 <<< + LNewHour
End If

This is a bit long-winded, but seems to work: 这有点费劲,但似乎可行:

If Weekday(modDropOff) = 6 And Hour(modDropOff) >= 16 Then
    modDropOff = DateSerial(Year(modDropOff), Month(modDropOff), Day(modDropOff)) + 3 + TimeSerial(7, 0, 0)
End If

Some ways to edit dates: 一些编辑日期的方法:

Option Explicit

Public Sub checkDate()

    Dim modDropOff As Date

    If Weekday(Now) = 2 And Hour(Now) >= 16 Then

        modDropOff = (Date + 3) + TimeValue("07:00:00 AM")

    Else

        modDropOff = Now

    End If

    MsgBox Format(modDropOff, "ddd mmm dd, yyyy - hh:mm:ss AMPM")

End Sub

Public Sub editDate()
    Dim modDropOff As Date
    If Weekday(Now) = 2 And Hour(Now) >= 16 Then
        modDropOff = DateAdd("d", 3, Now)
        modDropOff = DateSerial(Year(Now), Month(Now), Day(Now) + 3) + TimeSerial(7, 0, 0)
    Else
        modDropOff = Time
    End If
    MsgBox Format(xDate, "ddd mmm dd, yyyy - hh:mm:ss AMPM")
End Sub

.

Other VBA Date and Time functions 其他VBA日期和时间功能

  • Date - current system Date (Date part only) 日期-当前系统日期(仅日期部分)
  • Now - current system Date and Time 现在-当前系统日期和时间
  • Year, Month, MonthName, WeekDay, WeekdayName, Day 年,月,月名称,周日,周日名称,天
  • Hour, Minute, Second 时,分,秒
  • IsDate, CDate, DateSerial, DateValue, DateAdd, DateDiff, DatePart IsDate,CDate,DateSerial,DateValue,DateAdd,DateDiff,DatePart
  • Time, TimeSerial, TimeValue 时间,时间序列,时间值
  • FormatDateTime, Timer FormatDateTime,计时器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM