parsing date string

Z

z. f.

HI,
i have string in format
dd/mm/yyyyy hh:mm:ss
and giving this as an input to DateTime.Parse gives a string was not
recognized as a valid date time format string error.
how do i make the parse method to accept formating that i need.
if i do console.writeLine DateTime.Now.ToString it gives the format:
YYYY-MM-DD HH:MM:SS

TIA, Z.
 
S

Shiva

Did you try DateTime.ParseExact()?

HI,
i have string in format
dd/mm/yyyyy hh:mm:ss
and giving this as an input to DateTime.Parse gives a string was not
recognized as a valid date time format string error.
how do i make the parse method to accept formating that i need.
if i do console.writeLine DateTime.Now.ToString it gives the format:
YYYY-MM-DD HH:MM:SS

TIA, Z.
 
Z

z. f.

isn't there a simple function that can accept the format i'm using
(dd/mm/yyyy )
i saw in MSDN some complex objects for managing the culture and format but
didnn't see a simple way to parse using a format like sql convert function
allows.
i hope somene posts here a sample of parsing date and stating which format
to use.
 
H

Herfried K. Wagner [MVP]

z. f. said:
i have string in format
dd/mm/yyyyy hh:mm:ss
and giving this as an input to DateTime.Parse gives a string was not
recognized as a valid date time format string error.
how do i make the parse method to accept formating that i need.
if i do console.writeLine DateTime.Now.ToString it gives the format:
YYYY-MM-DD HH:MM:SS

Take a look at 'DateTime.ParseExact'. There you can specify the format of
the date you want to parse. Notice that your format string is invalid.

Date and Time Format Strings
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconDateTimeFormatStrings.asp>
 
C

Cor Ligthert

dd/mm/yyyyy hh:mm:ss

Where in Austria, here in Holland goes this very well?

MessageBox.Show(CDate("18/10/2004 15:40:00").ToString())

I am really curious what time system they use in Austria?

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Where in Austria, here in Holland goes this very well?

There is a difference between "mm" and "MM"...
MessageBox.Show(CDate("18/10/2004 15:40:00").ToString())

I am really curious what time system they use in Austria?

It works in Austria, but not in the US:

\\\
System.Threading.Thread.CurrentThread.CurrentCulture = _
New System.Globalization.CultureInfo("en-US")
MessageBox.Show(CDate("18/10/2004 15:40:00").ToString())
///

.... throws an 'InvalidCastException'.
 
C

Cor Ligthert

Herfried,
It works in Austria, but not in the US:

I am glad that you tell me that, you could not know that I knew that
already.

However z.f. is has a mail extention IL so probably he is from Isreal, I do
not know those settings, however I find it again clever from you that you
even knows those culture settings exactly.

:)))

Cor.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
However z.f. is has a mail extention IL so probably he is from Isreal, I
do not know those settings, however I find it again clever from you that
you even knows those culture settings exactly.

That's the reason why I prefer 'ParseExact' -- it doesn't require specific
culture settings...
 
C

Cor Ligthert

That's the reason why I prefer 'ParseExact' -- it doesn't require specific
culture settings...
You will say that with ParseExact it is forever working whatever
culturesettings your computer has?

So when somebody uses the program in the US and types in 01-11-2004 and
somebody uses it in Austria 11-01-2004 the computer knows with your sample
exactly that it has to be the 11th of november 2004.

Strange I thought that for that you needed CDate.

But when you say that this is working everywhere on the earth who am I that
I will not believe you.

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
You will say that with ParseExact it is forever working whatever
culturesettings your computer has?

So when somebody uses the program in the US and types in 01-11-2004 and
somebody uses it in Austria 11-01-2004 the computer knows with your sample
exactly that it has to be the 11th of november 2004.

Strange I thought that for that you needed CDate.

But when you say that this is working everywhere on the earth who am I
that I will not believe you.

Yes, it will work on every machine that supports the .NET Framework, if you
specify the date format in 'ParseExact' correctly.
 
C

Cor Ligthert

Yes, it will work on every machine that supports the .NET Framework, if
you
specify the date format in 'ParseExact' correctly.
Can you give me an example that will work in the same program in the US and
Austria.

Because I become completly confused about this, maybe is it to difficult for
me and will a little sample make it more clear for me.

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Can you give me an example that will work in the same
program in the US and Austria.

\\\
MsgBox( _
Date.ParseExact( _
"01-11-2000", _
"dd-MM-yyyy", _
Nothing _
).ToString() _
)
///
 
C

Cor Ligthert

\\\
MsgBox( _
Date.ParseExact( _
"01-11-2000", _
"dd-MM-yyyy", _
Nothing _
).ToString() _
)
///
Does that give the right date in the US notation, I thought you wrote it was
something else in this thread, this is the Dutch notation. However much
shorter with CDate.

:))))))


Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Does that give the right date in the US notation, I thought
you wrote it was something else in this thread, this is the
Dutch notation. However much shorter with CDate.

?!?

There are systems that do not use Dutch as system culture...

:)))
 
A

Alvaro Lamas Castillo

After everything I have read, the real problem or difference rather is not
that the code works in one place and does not in the other, the thing is the
regional settings of windows. as long as that is set to the date format
required everything should work, the thing is that in the US the normal date
format is mm/dd/yyyy while in a lot of other places here included its
dd/mm/yyyy.
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
The point is that ParseExact parses the date exactly in the format given,
CDate & DateTime.Parse parse the date based on the current region/local...

To better see what is happening use DateTime.ToLongDateString instead of
DateTime.ToString:

Const s1 As String = "01-11-2000"
Const s2 As String = "11-01-2000"

Const mdy As String = "MM-dd-yyyy"
Const dmy As String = "dd-MM-yyyy"

Debug.WriteLine(Date.ParseExact(s1, mdy,
Nothing).ToLongDateString(), "s1 mdy")
Debug.WriteLine(Date.ParseExact(s1, dmy,
Nothing).ToLongDateString(), "s1 dmy")
Debug.WriteLine(Date.ParseExact(s2, mdy,
Nothing).ToLongDateString(), "s2 mdy")
Debug.WriteLine(Date.ParseExact(s2, dmy,
Nothing).ToLongDateString(), "s2 dmy")

Hope this helps
Jay
 
C

Cor Ligthert

Jay,

I know that however I try to tell everytime "use the globalization and do
not use fixed dateformatstrings for the right date" and Herfried comes
everytime with those fixed date setting.

Therefore my messages about this.

Cor
 
C

Cor Ligthert

Alvaro,

I know that very well, this thread has a predecessor at the end I showed
this to Herfried, when it was really needed in a non US Anglo Canadian
setting to use such a date in another culture.

\\\
Public Class Example
Public Shared Sub Main()
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo("en-US")
Dim mydate As DateTime = CDate("10/16/2004")
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
MessageBox.Show(mydate.ToString)
End Sub
End Class
//

This is crazy code, however it was to show how many possibilities there are
with globalization setting to do it better.

And than Herfrieds answer was.
Well, this should work, and that's what I missed in your post, but I think
that 'DateTime.ParseExact' is much faster to type...

So therefore this long thread to get the change when he had given a complete
answer (what he did not) to write

"This goes much shorter with CDate".

:)

Cor
 

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