Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am confusing, Why should we use datetime.parse() instead of convert.toInt32()
Please give me an answer with example.
Posted
Comments
Zoltán Zörgő 10-Apr-13 4:10am    
I am confused too: datetime.parse will read s string representation of a datetime and make it a datetime object; while convert.toInt32 will convert to int32 a lot of things. So these two are not replacements of each other. Your question makes no sense.
Thomas Daniels 10-Apr-13 4:15am    
I edited your comment, because you wrote 'convert.toInt30' instead of 'convert.toInt32'

I would say, this two methods have not much in common.

VB
public static DateTime Parse(
    string s
)


Converts the string representation of a date and time to its DateTime equivalent.

Return Value
Type: System.DateTime
An object that is equivalent to the date and time contained in s.

C#
public static int ToInt32(
    string value
)


Converts the specified string representation of a number to an equivalent 32-bit signed integer.

Return Value
Type: System.Int32
A 32-bit signed integer that is equivalent to the number in value, or 0 (zero) if value is null.

In other words, you use the first method to convert a string like "01.May 2013 17:45:25" to DateTime and the second one to convert a string like "-25" to an integer!
 
Share this answer
 
Comments
Thomas Daniels 10-Apr-13 5:04am    
+5!
Leo Chapiro 10-Apr-13 6:05am    
Thanks a lot, ProgramFOX! :)
du[DE] is right, but if you mean the difference between int.Parse and Convert.ToInt32, then have a look here:
http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32[^]
And if you mean the difference between DateTime.Parse and Convert.ToDateTime, then have a look here:
http://stackoverflow.com/questions/8192520/any-difference-between-parse-and-convert[^]
http://forums.asp.net/t/1710269.aspx/1[^]
http://www.pcreview.co.uk/forums/convert-todatetime-vs-system-datetime-parse-t2509851.html[^]
 
Share this answer
 
Comments
Leo Chapiro 10-Apr-13 4:20am    
+5 :)
Thomas Daniels 10-Apr-13 4:22am    
Thank you!
You should use the Parse method when you have the string representation of a date or time (or both) and need to convert it into a DateTime object (see DateTime.Parse Method[^].
On the other hand, DatetTime.IConvertible.ToInt32[^] would go in the opposite direction (it would actually throw, as stated by the documentation).
 
Share this answer
 
v2
Comments
Leo Chapiro 10-Apr-13 4:15am    
Thats it! +5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900