Question convert type string to System.DateTime. How?

mikrophun

Member
Joined
Jan 22, 2013
Messages
7
Programming Experience
Beginner
Hi,

i have a problem with string conversion.
i have string which value is "2012-02-23 15:00:00" and i want to covert it to DateTime with format like "6/10/2013 2:00:00 PM".

is there any solution for this.

Regards,
mik
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
DateTimes don't have a format. DateTimes are just a number. If you want to display the date/time value it contains in a particular format then you have to convert it to a String. So, it would seem that what you're really saying is that you have a String containing a date/time in one format and you want to convert that to a String containing the same date/time in another format. The way to do that is to convert the first String to a DateTime and then convert that to a String.

The DateTime type has four methods for converting Strings to DateTimes: Parse, TryParse, ParseExact and TryParseExact. The ones that start with "Try" are for when you don;t know whether the String is valid or not and the ones that end with Exact are when you know the specific format(s) that the input is in. Just pick the one that suits your scenario. Once you have a DateTime, call its ToString method and pass the format specifier for the output you want.
 
Top Bottom