5

I have a string value which is a datetime : "20100825161500" and I want to convert this to a System Datetime. I have tried Convert.ToDateTime and DateTime.Parse and these do not work.

3 Answers 3

14

You can use DateTime.ParseExact to pass the format you need.

Here is an example:

var parsed = DateTime.ParseExact("20100825161500","yyyyMMddHHmmss", null);

Possible format values are listed at Standard Date and Time Format Strings and Custom Date and Time Format Strings

Sign up to request clarification or add additional context in comments.

Comments

4

Try to use something like this

Datetime D = DateTime.ParseExact("20100825161500","yyyymmdd...",null)

here you have a link about how to make you "format" string

Comments

-2

Because this string hasn't a format recognized by these 2 functions.

DateTime.Parse and Convert.ToDateTime require your string to be correctly formatted : http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

http://msdn.microsoft.com/en-us/library/xhz1w05e.aspx

You will have to write your custom parser for this kind of conversion

2 Comments

-1: DateTime.ParseExact allows a format to be specified, you don't need to write this yourself.
I didn't know ParseExact. Thanks for the info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.