Saturday, January 12, 2008

DateTime Parse & TryParse

1. Using Convert when the value is null, it will set to its default value, whereas using Parse will throw an exception

Dim strDT As String = Nothing

Dim dt As DateTime = Convert.ToDateTime(strDT) 'Result: 1/01/0001 12:00:00 AM

Dim dt1 As DateTime = DateTime.Parse(strDT) 'Result: ArgumentNullException

Console.WriteLine(dt.ToString(), Environment.NewLine)

Dim strA As String = Nothing

Dim intResult As Integer = Convert.ToInt32(strA) 'Result: 0

Dim intResult1 As Integer = Integer.Parse(strA) 'Result: ArgumentNullException

Console.WriteLine(intResult)

Console.ReadLine()

Note:
Convert.ToDateTime("") or DateTime.Parse("") will both throw an exception (String was not recognized as a valid DateTime.)


2. TryParse: Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded. The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It has the flexibility to set cultureinfo as well.


Dim result As Boolean

Dim datetimeVal As DateTime

'result = DateTime.TryParse("7/12/2004 12:34:56", datetimeVal)

Dim ci As CultureInfo = New CultureInfo("en-US")

result = DateTime.TryParse("4/7/2004 12:34:56", ci, DateTimeStyles.None, datetimeVal)

If result Then

Console.WriteLine(datetimeVal.ToString("dd-MMM-yyyy")) '07-Apr-2004

Else

Console.WriteLine("Parse failed.")

End If

Console.ReadLine()

Int32.TryParse Method


3. This article

Comparing String to Integer Conversion Methods in VB.NET

will compare all the methods which converts string to integer, such as:

1. Val

2. CInt

3. Parse

4.TryParse

5. Convert.ToInt32

The author recommends using Parse & TryParse which have the best Usability, Flexibility and Performance.


4. DateTime samples:


Dim dtNow As DateTime = DateTime.Now

Console.WriteLine("dtNow.ToString: " + dtNow.ToString)

Console.WriteLine("dtNow.Date: " + dtNow.Date)

Console.WriteLine("dtNow.ToShortDateString: " + dtNow.ToShortDateString)

Console.WriteLine("dtNow.ToLongDateString: " + dtNow.ToLongDateString)

Console.WriteLine("dtNow.ToShortTimeString: " + dtNow.ToShortTimeString)

Console.WriteLine("dtNow.ToLongTimeString: " + dtNow.ToLongTimeString)

Console.WriteLine("dtNow.ToUniversalTime: " + dtNow.ToUniversalTime)

Console.WriteLine("dtNow.DayOfYear: " + dtNow.DayOfYear.ToString)

Console.WriteLine("dtNow.DayOfWeek: " + dtNow.DayOfWeek.ToString)

Console.WriteLine("dtNow.Day: " + dtNow.Day.ToString)

Console.WriteLine("dtNow.Month: " + dtNow.Month.ToString)

Console.WriteLine("dtNow.Year: " + dtNow.Year.ToString)

Console.WriteLine("dtNow.AddDays(1): " + dtNow.AddDays(1))

Console.ReadLine()

Result:


dtNow.ToString: 13/01/2008 12:17:09 PM
dtNow.Date: 13/01/2008
dtNow.ToShortDateString: 13/01/2008
dtNow.ToLongDateString: Sunday, 13 January 2008
dtNow.ToShortTimeString: 12:17 PM
dtNow.ToLongTimeString: 12:17:09 PM
dtNow.ToUniversalTime: 13/01/2008 1:17:09 AM
dtNow.DayOfYear: 13
dtNow.DayOfWeek : Sunday()
dtNow.Day: 13
dtNow.Month: 1
dtNow.Year: 2008
dtNow.AddDays(1): 14/01/2008 12:17:09 PM

This article String Formatting in C# has all the basic number formatting specifiers.

blog comments powered by Disqus