473,396 Members | 1,749 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,396 software developers and data experts.

DateTime parsing

Hi all

If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like IsDate in VB.Net. Would it be a good idea to use a static method in a static object to convert the dateTime to DateTime datatype as follows

public static DateTime ParseDate(String dateTime

tr

return DateTime.Parse(dateTime)

catc

return null; // Not sure about what to return, error occurs

}

Does the use of the static method cause the performance of the application decrease? if so, for the case above, should I just put the content of the static method at the original method(the point of use)
Would it be always a good practice not to make any assumption and test the validity of the parameter of the method

Thanks for your hel

Simon
Nov 22 '05 #1
4 3581

"Simon" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
Hi all,

If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like
IsDate in VB.Net. Would it be a good idea to use a static method in a static
object to convert the dateTime to DateTime datatype as follows:
public static DateTime ParseDate(String dateTime)
{
try
{
return DateTime.Parse(dateTime);
}
catch
{
return null; // Not sure about what to return, error occurs.
}
}

Does the use of the static method cause the performance of the application decrease?

No.

if so, for the case above, should I just put the content of the static
method at the original method(the point of use)? Would it be always a good practice not to make any assumption and test the

validity of the parameter of the method?

Sure, you might test it for null value or empty string - depends on your
needs.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Nov 22 '05 #2
http://www.dotnet247.com/247referenc.../17/85604.aspx has some code for
this as well. The method there returns a boolean, which is a better fit for
IsDate().

Otherwise you may be better off just using DateTime.Parse() or
Convert.ToDateTime() instead of having this method. With your current method
you still have to check for a null return, so you might as well just not
have the method and do exception handling in main code (especially since
you'd be doing it anyway in the method).

"Simon" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
Hi all,

If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like
IsDate in VB.Net. Would it be a good idea to use a static method in a static
object to convert the dateTime to DateTime datatype as follows:
public static DateTime ParseDate(String dateTime)
{
try
{
return DateTime.Parse(dateTime);
}
catch
{
return null; // Not sure about what to return, error occurs.
}
}

Does the use of the static method cause the performance of the application decrease? if so, for the case above, should I just put the content of the
static method at the original method(the point of use)? Would it be always a good practice not to make any assumption and test the validity of the parameter of the method?
Thanks for your help

Simon

Nov 22 '05 #3

"Simon" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
Hi all,

If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like
IsDate in VB.Net. Would it be a good idea to use a static method in a static
object to convert the dateTime to DateTime datatype as follows:
public static DateTime ParseDate(String dateTime)
{
try
{
return DateTime.Parse(dateTime);
}
catch
{
return null; // Not sure about what to return, error occurs.
}
}

Does the use of the static method cause the performance of the application decrease?

No.

if so, for the case above, should I just put the content of the static
method at the original method(the point of use)? Would it be always a good practice not to make any assumption and test the

validity of the parameter of the method?

Sure, you might test it for null value or empty string - depends on your
needs.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Nov 22 '05 #4
http://www.dotnet247.com/247referenc.../17/85604.aspx has some code for
this as well. The method there returns a boolean, which is a better fit for
IsDate().

Otherwise you may be better off just using DateTime.Parse() or
Convert.ToDateTime() instead of having this method. With your current method
you still have to check for a null return, so you might as well just not
have the method and do exception handling in main code (especially since
you'd be doing it anyway in the method).

"Simon" <si************@yahoo.com> wrote in message
news:95**********************************@microsof t.com...
Hi all,

If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like
IsDate in VB.Net. Would it be a good idea to use a static method in a static
object to convert the dateTime to DateTime datatype as follows:
public static DateTime ParseDate(String dateTime)
{
try
{
return DateTime.Parse(dateTime);
}
catch
{
return null; // Not sure about what to return, error occurs.
}
}

Does the use of the static method cause the performance of the application decrease? if so, for the case above, should I just put the content of the
static method at the original method(the point of use)? Would it be always a good practice not to make any assumption and test the validity of the parameter of the method?
Thanks for your help

Simon

Nov 22 '05 #5

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

Similar topics

5
by: Gerrit Holl | last post by:
Hi, it seems the datetime library does not have a .strptime class method. If I want to create a datetime object from a string, I currently have to do: datetime.date(*time.strptime(s,...
0
by: F. GEIGER | last post by:
py2exe and datetime -> No module named datetime I've begun to use the stdlib module datetime instead of my home brewn classes. Since then a py2exe app doesn't run anymore: Traceback (most...
2
by: Simon | last post by:
Hi all If I have a method for inserting data to database and it take a parameter dateTime in String. Since C# does not have a function to test date like IsDate in VB.Net. Would it be a good idea...
15
by: Dan S | last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling...
2
by: Sterling Ledet | last post by:
I am trying to create a web service that takes a string from my web server in the following format: Mon, 6 Oct 2003 18:39:47 UTC and put's in a datetime so it can then be reformatted in C# as...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
26
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM'...
4
by: Michael Meckelein | last post by:
Hello, Wondering, if C# (framework 2.0) does not support parsing DateTime timezones in three letter acronyms. I would like to parse date strings like "2005 Nov 01 11:58:47.490 CST -6:00" but...
6
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm just trying to test datetime functions: DateTime dt1 = "9/15/2007"; DateTime dt2 = "9/17/2007"; DateTime dtResult; dtResult = dt2 - dt1; The above is what i'm trying to say...
7
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.