Satbir
Satbir

Reputation: 6496

DateTime Parse from String with specified format

I am trying to extract datetime info from

"2012/04/03 10:06:21:611747"

using format

String dateformat = @"yyyy/mm/dd hh:mm:ss:ffffff";

Getting an exception - any help is appreciated.

Full code

 String dateformat = @"yyyy/mm/dd hh:mm:ss:ffffff";
 readonly CultureInfo _provider = CultureInfo.InvariantCulture;
 DateTime dateTime = DateTime.ParseExact(line, dateformat, _provider);

Upvotes: 1

Views: 1625

Answers (5)

vikrantx
vikrantx

Reputation: 603

Replace mm with MM

This link will help

Upvotes: 1

Akanksha Gaur
Akanksha Gaur

Reputation: 2676

Use

String dateformat = @"yyyy/MM/dd hh:mm:ss:ffffff";  

Use MM for month.
The exception was ocurring due to confusion in interpreting m at two different places.

Upvotes: 1

ionden
ionden

Reputation: 12776

Use MM for months:

 String dateformat = @"yyyy/MM/dd hh:mm:ss:ffffff";

Upvotes: 7

Darbio
Darbio

Reputation: 11418

Your format should be:

yyyy/MM/dd hh:mm:ss:ffffff

You have a lower case m for month which is incorrect.

Have a look at this answer for more info: .Net: DateTime String format

Upvotes: 5

Marco
Marco

Reputation: 57573

You should use

String dateformat = @"yyyy/MM/dd hh:mm:ss:ffffff";

MM is for month, while mm is for minutes!!

Upvotes: 3

Related Questions