473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Validate Date before casting

Hi,

I have what is probabally a date contained in an object variable.
Before casting to date I actually want to attempt to verify that I actually
have a date.
I prefer to do this before casting as I do not want to generate an exception
if the object is NOT a date.
VB has the IsDate() function.. Is there an equivilent in c#, I'd prefer not
to have to reference the vb library just for this function.

cheers

martin.
Nov 16 '05 #1
3 2976
Martin,

object time = DateTime.Now;

bool isDate = (time.GetType() == typeof(DateTime));

Is this enough?

HTH,
Alexander Shirshov

"Martin" <ma*******************@martinz.co.nz> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I have what is probabally a date contained in an object variable.
Before casting to date I actually want to attempt to verify that I
actually have a date.
I prefer to do this before casting as I do not want to generate an
exception if the object is NOT a date.
VB has the IsDate() function.. Is there an equivilent in c#, I'd prefer
not to have to reference the vb library just for this function.

cheers

martin.

Nov 16 '05 #2
Martin,

You can set the parse in a try catch block. Probably (almost sure) is in
VBNet the same done with the IsDate.

Cor
Nov 16 '05 #3
Thanks for the solution,

I guess that I can use this to test any variable types.

cheers

martin.
"Alexander Shirshov" <al*******@omnitalented.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Martin,

object time = DateTime.Now;

bool isDate = (time.GetType() == typeof(DateTime));

Is this enough?

HTH,
Alexander Shirshov

"Martin" <ma*******************@martinz.co.nz> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I have what is probabally a date contained in an object variable.
Before casting to date I actually want to attempt to verify that I
actually have a date.
I prefer to do this before casting as I do not want to generate an
exception if the object is NOT a date.
VB has the IsDate() function.. Is there an equivilent in c#, I'd prefer
not to have to reference the vb library just for this function.

cheers

martin.


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alexandre | last post by:
Hi again, What's the best I can do to validate fields like date before send my datas. thx
4
by: Frank Rocco | last post by:
Hello, What is the best way to validate a textbox to see if it contains a valid date or a valid time or both? Thanks Frank
6
by: RSB | last post by:
Hi Every one Need some help to Validate the Date i have in the Form. ALso how to Convert the Date Data i read from table to yyyy/MMM/dd format. Thanks RSB
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
12
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
4
by: Michel Posseth [MCP] | last post by:
I have a problem with the date time picker validate event wich i believe is a bug How to reproduce : throw on a form a date time picker control and a textbox control select the validating...
3
by: samuelberthelot | last post by:
Hello, I would like to validate a date in a textbox on the onChange event. The date must be in the format 01/01/2007 I would like to use a regular expression to validate it but I'm not very...
2
by: Mick Walker | last post by:
Hi, I have a problem that I have been trying to figure for a couple of days, and am wondering if anyone out there would be so kind as to give me a solution. (Deadline time) I am trying to...
5
by: shapper | last post by:
Hello, What is the Regex expression to validate a date time format as follows: dd-mm-yyyy hh:mm:ss An example: 20-10-2008 10:32:45
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.