Siva Krishna Beram

HI Everybody,

I am having a problem in converting a string to date time.

string s ="01 JAN 2007 23 00" in which time part is in the format of 24Hrs clock.

When i am using Convert.ToDateTime(s) function, it is working till i am passing the vlaue of Hours less than 13 i.e 1pm.

Please come with your comments and sloutions.


Re: Visual C# General Covert.ToDateTime

OmegaMan

Siva Krishna Beram wrote:

HI Everybody,

I am having a problem in converting a string to date time.

string s ="01 JAN 2007 23 00" in which time part is in the format of 24Hrs clock.

When i am using Convert.ToDateTime(s) function, it is working till i am passing the vlaue of Hours less than 13 i.e 1pm.

Please come with your comments and sloutions.


So you are saying when

"01 JAN 2007 01 00"

should be 1 pm and not 1 am How is the parser to know that intention Is there something missing in your post





Re: Visual C# General Covert.ToDateTime

timvw

I would recommend that you use the ParseExact method instead... At first sight the following format seems to do what you want...

Code Snippet

static void Main(string[] args)
{
string input = "1 JAN 2007 23 00";
string format = "d MMM yyyy HH mm";

DateTime dateTime = DateTime.ParseExact(input, format, CultureInfo.InvariantCulture);
Console.WriteLine(dateTime.ToString("R"));

Console.Write("{0}Press any key to continue...", Environment.NewLine);
Console.ReadKey();
}


Otherwise you'll have to lookup the format... I usually use the info at: http://blog.stevex.net/index.php/parsing-dates-and-times-in-net/