Reputation: 359
I have a Date
variable which contains also the time:
Dim dt As Date
dt = "8/3/2016 7:10:40 AM"
--> remove somehow the time so the result should be:
dt = "8/3/2016"
How to remove the time?
Upvotes: 7
Views: 6548
Reputation: 2080
Dim dateonly As String
dateonly = Left(dt, InStr(dt, " ")-1)
You can make it safer by adding a test :
Dim dt As String
Dim pos As Integer
Dim dateonly As String
dt = "8/6/2016"
pos = InStr(dt, " ")
If pos Then
dateonly = Left(dt, pos - 1)
Else
dateonly = dt
End If
Debug.Print dateonly
Upvotes: 1
Reputation: 175766
For another Date type variable:
dt = DateValue(dt)
or
dt = CDate(Int(dt))
Or for a string:
strdt = format$(dt, "m/d/yyyy")
Upvotes: 9