DateTime.ParseExact problem with parsing "8/09/05 9:27a"

R

ramon.smits

I'm trying to get the following date/time string formatted. But I don't
succeed. I get an exception saying the date/time string is invalid.

string input = "8/09/05 9:27a";
string format = "M/dd/yy h:mmt";

DateTime.ParseExact(input, format, null);

What am I doing wrong here? The following parse works ok.

string input = "9-08-05 9:27";
string format = "d-MM-yy H:mm";
 
G

Guest

The follow *should* work for you but I'm not sure how it handles
globalization as I'm doing this from memory.

===============
using System.Globalization;
....
....
DateTimeFormatInfo formatInfo = new DateTimeFormatInfo();
formatInfo.AMDesignator = "am";

string input = "8/09/05 9:27a";
string format = "M/dd/yy h:mmt";
DateTime.ParseExact(input, format, formatInfo)
===============

Hope this helps.

Brian Delahunty
Ireland

http://briandela.com/blog
 
W

Wessel Troost

string input = "8/09/05 9:27a";
string format = "M/dd/yy h:mmt";
DateTime.ParseExact(input, format, null);


The "t" format specifier expects a capital A. The following input parses
fine:

string input = "8/09/05 9:27A";

Gretings,
Wessel
 
R

Ramon Smits aka Exyll

Capitilizing didn't help. I needed to add a DateTimeFormatInfo where I
specified the AM/PMDesignator. The anwser of Brian Delahunty works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top