Convert a string to a datetime using a pattern

  • Thread starter Thread starter Iwan Petrow
  • Start date Start date
I

Iwan Petrow

Hi,

I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm".
How could I convert this string to DateTime type (I prefer somehow to
set this as a pattern if it is possible because I have other
possibilities as "dd~MM~yyyy, hh:mm")?

Thanks.
 
string myString = "1-1-2000";
DateTime myDateTime = DateTime.Parse(myString);

Hope this helps,
Arjen
 
I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm".
How could I convert this string to DateTime type (I prefer somehow to
set this as a pattern if it is possible because I have other
possibilities as "dd~MM~yyyy, hh:mm")?

DateTime.Parse(...)
 
Thanks.

But I want culture invariant.

I've found a class DateTimeFormatInfo which is good enough but I want
to forbid AM and PM. I use HH but it doesn't work (it accepts them).

Any ideas for forbidding them (AM and PM)?
 
Hi,

One of the overloads of DateTime.ParseExact does it:

DateTime.ParseExact( "31-.12-.2005 23:51", "dd-.MM-.yyyy hh-.mm" ,
DateTimeFormatInfo.InvariantInfo );

Note:
I wrote the above line from memory, it may have some error in the format,
check it first, also note that the casing is important MM=month and
mm=minutes

Cheers,
 
Iwan Petrow said:
But I want culture invariant.

In that case, pass the invariant culture into DateTime.ParseExact.
I've found a class DateTimeFormatInfo which is good enough but I want
to forbid AM and PM. I use HH but it doesn't work (it accepts them).

Any ideas for forbidding them (AM and PM)?

Have you tried using ParseExact rather than just Parse?
 
Thanks

It works in most cases. But for this there is a problem:
DateTime.ParseExact( "31\\12\\2005", "dd\\MM\\yyyy" ,
DateTimeFormatInfo.InvariantIn­fo );

It throws exception with message "string is not a date" or something
similar.
Why? Any solutions?
 
Hi,

haa , tricky one :) , but easy to explain

if you look the help of DateTimeFormatInfo you will see at the botton that
there is a \c variant :
\ c Where c is any character. Displays the character
literally. To display the backslash character, use "\\".

So, if you change the expression to:

DateTime d = DateTime.ParseExact( @"31\12\2005", @"dd\\MM\\yyyy" ,
DateTimeFormatInfo.InvariantInfo );

you will be ok, see that I use the @ at the start, IMO it's clearer that
using two \


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




Thanks

It works in most cases. But for this there is a problem:
DateTime.ParseExact( "31\\12\\2005", "dd\\MM\\yyyy" ,
DateTimeFormatInfo.InvariantInfo );

It throws exception with message "string is not a date" or something
similar.
Why? Any solutions?
 

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

Back
Top