Determining whether a string is a date, time, or date and time

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

When determining whether a String can be converted to a DateTime, you can
use the IsDate() method. However, I would also like to know whether the
string is a date, a time, or both a date and a time. Is there any simple way
to do this without using manual pattern matching? Thanks.
 
When determining whether a String can be converted to a DateTime, you
can use the IsDate() method. However, I would also like to know
whether the string is a date, a time, or both a date and a time. Is
there any simple way to do this without using manual pattern matching?
Thanks.

Perhaps you can use the Convert.ToDate() function? Once you convert it to a
date, you can check the .Date and .Time properties to determine the
original values?
 
This would not work. The reason I need to know this information is so that I
know whether to include the date, time, or both when submitting the data to
a database. The reason your suggestion will not work is because a Date
object ALWAYS has a date and time, even if the date is the default date or
the time is 0:00 or 12:00 or whatever. Therefore, looking at these parts of
the Date will tell me nothing about whether a date, time, or both were
included in the original String.
 
The short answer to your original question is no.

If you want the 'mechanism' to be a one size fits all multi-purpose adjuster
then you will just have to roll your own convoluted pattern matching code.

If you know things in advance about the format(s) of the strings then it is
not difficult to cater for the permutations.

The suggestion is perfectly valid given certain simple assumptions.

If the conversion fails then the string does not contain a recognisible
date/time.

If the date part of the result is DateTime.MinValue.Today then the string
only contained a time.

If the time part of the result is midnight then the string only contained a
date.

Otherwise the string contained both parts.

Now, such simple assumption might not happen to suit you in which case
you're back to rolling your own.
 
Somehow I was afraid that might be the final answer. But since I don't want
to write regular expressions for EVERY possible scenario (I'd probably end
up forgetting some with all the possible formats dates and times can be
written in), I decided to use the following conditional statement:

If original.Contains(":") Then
If original.Contains("/") OrElse original.Contains("-") Then
'Contains both date and time
Else
'Contains only time
End If
Else
'Contains only date
End If

This statement assumes that the : is always used for times, and either a /
or - is always used for dates. Since the original string will be coming from
users of a website, I am guessing that this can usually be assumed to be
true (and I can always add a RegularExpressionValidator). Does this sound
like a good solution to you aside from creating regular expressions for
every possible scenario?
 
Nathan,

Just creating a different box for the date and time string makes things a
lot easier.
Especially on a website where this is even more common.

There are in different cultures two kind of clock registrations used.

With an 24 hour clock and a 12 hour clock even about this you have to take
care of.

Cor
 
Back
Top