简体   繁体   中英

Excel VBA Adding 1 hour to a date / time string

I have a text string that looks like this: "06/10/15 4:53pm". I want to add 1 hour (60 minutes exactly) to the text string looks like this: "06/10/15 5:53pm"

Any suggestions would be appreciated.

Convert it to a date, add an hour, and convert back to string using format

Private Sub TestIt()

    MsgBox AddHour("06/10/15 4:53pm")

End Sub

Public Function AddHour(ByVal sTime As String) As String
    Dim dt As Date

    dt = CDate(sTime)
    dt = DateAdd("h", 1, dt)

    AddHour = Format(dt, "mm/dd/yy h:nnam/pm")

End Function

Reference:

不需要VBA ...假设上方(06/10/15 4:53 pm)的时间值在单元格A1中,则您要查找的公式为:

=A1+TIME(1,0,0)

Since you asked for a VBA solution:

s = "06/10/15 4:53pm"

MsgBox CDate(s) + 1 / 24

For operations with date and time in VBA is DateAdd function. You can find how to use it in this link: https://www.techonthenet.com/excel/formulas/dateadd.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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