I need a way to check if the time portion of a date is populated in a datefield.
Datepart() returns 0 for both of the following:
DatePart("h", "11/14/2003 00:00:00"
DatePart("h", "11/14/2003"
The first one has midnight as the time. The second one does not have a time, but midnight is still implied. I want a concrete way to know if a time is provided (as I mentioned, both examples return the same value).
My current solution is to use instr(1, "11/14/2003", ":") to see if a colon is present. Any one else got a better way?
Thanks,
Orrin
Comments
:
: Datepart() returns 0 for both of the following:
: DatePart("h", "11/14/2003 00:00:00"
: DatePart("h", "11/14/2003"
:
: The first one has midnight as the time. The second one does not have a time, but midnight is still implied. I want a concrete way to know if a time is provided (as I mentioned, both examples return the same value).
:
: My current solution is to use instr(1, "11/14/2003", ":") to see if a colon is present. Any one else got a better way?
:
Ugh. I can't see any way to use VBs date/time functions to test for this, but I may be missing the right one. InStr will work fine, but I'd check for a space rather than the : (time seperator), which may or may not vary depending on the locale.
Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
: : I need a way to check if the time portion of a date is populated in a datefield.
: :
: : Datepart() returns 0 for both of the following:
: : DatePart("h", "11/14/2003 00:00:00"
: : DatePart("h", "11/14/2003"
: :
: : The first one has midnight as the time. The second one does not have a time, but midnight is still implied. I want a concrete way to know if a time is provided (as I mentioned, both examples return the same value).
: :
: : My current solution is to use instr(1, "11/14/2003", ":") to see if a colon is present. Any one else got a better way?
: :
: Ugh. I can't see any way to use VBs date/time functions to test for this, but I may be missing the right one. InStr will work fine, but I'd check for a space rather than the : (time seperator), which may or may not vary depending on the locale.
:
: Jonathan
:
: ###
: for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
:
Take advantage of errors!
[code]MsgBox DatePart("h", "11/14/2003" & " 00:00:00")
MsgBox DatePart("h", "11/14/2003 00:00:00" & " 00:00:00")
[/code]
The first returns 0, the second returns "Type mismatch". So append a "blank" time such as "00:00:00" (or any other time) and watch for the error.
: : : I need a way to check if the time portion of a date is populated in a datefield.
: : :
: : : Datepart() returns 0 for both of the following:
: : : DatePart("h", "11/14/2003 00:00:00"
: : : DatePart("h", "11/14/2003"
: : :
: : : The first one has midnight as the time. The second one does not have a time, but midnight is still implied. I want a concrete way to know if a time is provided (as I mentioned, both examples return the same value).
: : :
: : : My current solution is to use instr(1, "11/14/2003", ":") to see if a colon is present. Any one else got a better way?
: : :
: : Ugh. I can't see any way to use VBs date/time functions to test for this, but I may be missing the right one. InStr will work fine, but I'd check for a space rather than the : (time seperator), which may or may not vary depending on the locale.
: :
: : Jonathan
: :
: : ###
: : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
: :
: :
:
: Take advantage of errors!
:
: [code]MsgBox DatePart("h", "11/14/2003" & " 00:00:00")
: MsgBox DatePart("h", "11/14/2003 00:00:00" & " 00:00:00")
: [/code]
:
: The first returns 0, the second returns "Type mismatch". So append a "blank" time such as "00:00:00" (or any other time) and watch for the error.
:
And I thought the InStr() way was ugly. VB's error catching isn't amazing, so you'll probably want to do this in a little function, like:-
[code]Function dateHasTime (theDate As Date) As Boolean
On Error Goto HasTime
Dim test As Date
test = theDate & " 00:00:00"
dateHasTime = False
Exit Function
HasTime:
dateHasTime = True
End Function[/code]
<<shudders>>
Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
: [code]Function dateHasTime (theDate As Date) As Boolean
: On Error Goto HasTime
: Dim test As Date
: test = theDate & " 00:00:00"
: dateHasTime = False
: Exit Function
:
: HasTime:
: dateHasTime = True
: End Function[/code]
:
: <<shudders>>
If we're doing it that way, we might as well pretty it up a little:
[code]Public Function HasTime(ByVal dt As String) As Boolean
Dim t As Date
On Error Resume Next
t = dt & " 00:00:00"
HasTime = Err.Number <> 0
End Function
[/code]
Your code won't work if the passed date has midnight for the time; the midnight part will be dropped (when converting to a date for the parameter) and then appending a time will always be legal. Mine appends the time before converting to a date.