converting a date format

  • Thread starter Thread starter Reny J Joseph Thuthikattu
  • Start date Start date
R

Reny J Joseph Thuthikattu

?Hi,
i have a date string of format "mm/dd/yy".i want to convert it to systems
default format.how do i do that?
reny
 
Use DateTime.ParseExact() and call ToShortDateString() (or any method) on
the result. Eg:

DateTime.ParseExact ("01/10/04", "MM/dd/yy", Nothing).ToShortDateString()

?Hi,
i have a date string of format "mm/dd/yy".i want to convert it to systems
default format.how do i do that?
reny
 
Reny,

The VBNet advices method for that is
dim mydate as datetime = CDate(mydatestring)

Keep in mind that with every datetime conversion your globalsetting has to
be in the right datetime format.

I hope this helps?

Cor
 
Hi Herfried,

This URL is brokken, strange with you read as well the rest.
Read the docs for 'DateTime.Parse', 'DateTime.ParseExact' and
'DateTime.ToString', and this page:

Why this advice from you where CDate does all conversions correct including
database types?

Cor
 
Cor Ligthert said:
This URL is brokken, strange with you read as well the rest.


Why this advice from you where CDate does all conversions correct including
database types?

'CDate' is locale-aware and will return different results for "10/10/1999"
on machines with different date formats.
 
Herfried,
'CDate' is locale-aware and will return different results for "10/10/1999"
on machines with different date formats.

With this one I don't know what are the different ones, however I am very
glad that there is globalization, what you want VB6 formating?

Cor
 
Cor Ligthert said:
With this one I don't know what are the different ones, however I am very
glad that there is globalization, what you want VB6 formating?

The OP wanted to convert a string in a /certain/ format into a 'DateTime'.
That's a typical job for 'DateTime.ParseExact' where you can specify the
format of the string that should be parsed.
 
Herfried,

You know it of course already, I don't agree with you, however let's stop,
we can make a thread about this a mile long.

(I get more and more the idea that you will become a C# MVP)

:-))))

Cor
 
Cor Ligthert said:
You know it of course already, I don't agree with you,
however let's stop, we can make a thread about this a mile long.

I don't think so. 'CDate' is simply useless in this situation and doesn't
do what the OP wants to do.
(I get more and more the idea that you will become a C# MVP)

LOL! Definitely not.
 
I don't think so. 'CDate' is simply useless in this situation and doesn't
do what the OP wants to do.
Depends how you read it.

However when the OP means to the datetime format, than CDate is properiate,
when he means to converts from another globalized format, that is the only
place where you can be right.

However even than I would change temporaly the globalization settings to
make it definitly clear and use CDate.

Cor
 
Cor Ligthert said:
Depends how you read it.

I don't see any space for interpretation in the OP's post:

| i have a date string of format "mm/dd/yy".i want to
| convert it to systems default format.

The OP has a date string representing a date in a specific format, so
'CDate' is not the right joice.
 
Herfried,
"> The OP has a date string representing a date in a specific format, so
'CDate' is not the right joice.

I know that it is not your right joy, but I think the right method.

However when you do you mean that they have in Austria other globalization
settings so I am curious for that, can you try this and give me what it
gives as format.

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

Where this is the question from the OP
--------------
i have a date string of format "mm/dd/yy".i want to convert it to systems
default format.how do i do that?

And I have with the above example answered any possibility in my opinion,
however tell me than what you than think when it is not.

Cor
 
Cor Ligthert said:
"> The OP has a date string representing a date in a specific format, so

I know that it is not your right joy, but I think the right method.

However when you do you mean that they have in Austria other globalization
settings so I am curious for that, can you try this and give me what it
gives as format.

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

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

;-)
 
Back
Top