我有一个Date变量,其中也包含Time:
Dim dt As Date
dt = "8/3/2016 7:10:40 AM"
- >以某种方式删除时间,结果应该是:
dt = "8/3/2016"
我的问题是如何删除时间?
由于
答案 0 :(得分:3)
对于另一个Date类型变量:
dt = DateValue(dt)
或
dt = CDate(Int(dt))
或字符串:
strdt = format$(dt, "m/d/yyyy")
答案 1 :(得分:1)
Dim dateonly As String
dateonly = Left(dt, InStr(dt, " ")-1)
您可以通过添加测试来使其更安全:
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