Formatting a datetime value

  • Thread starter Thread starter Simon Osborn
  • Start date Start date
S

Simon Osborn

I'm trying to format a date and time value to a format that is not the same
as in the regional settings.

What I mean is:

Regional setting is set to US "MM/dd/yyyy".

I want to change the format to display as UK "dd/MM/yyyy".

So that I can make some comparisons later on in my program using UK format.

Everytime I try this I get an error.

Can anyone help me out!
 
* "Simon Osborn said:
I'm trying to format a date and time value to a format that is not the same
as in the regional settings.

What I mean is:

Regional setting is set to US "MM/dd/yyyy".

I want to change the format to display as UK "dd/MM/yyyy".

'DateTime.ToString(<format>)'.
 
Thanks for the speedy reply.

I've have tried that approach but it will only work if I format the datetime
the same as what is set in the regional settings.

Either that or or I'm doing something drastically wrong.
 
Hi Simon,



Take a look at 'date format' inside vs .net. There are several examples and
a full list of options - here's a few:

MyStr = Format(MyDateTime, "h:m:s") ' Returns "5:4:23".
MyStr = Format(MyDateTime, "hh:mm:ss tt") ' Returns "05:04:23 PM".
mdatestring = mdate.ToString("yyyyMMdd")



HTH,



Bernie Yaeger
 
I want to change the format to display as UK "dd/MM/yyyy".

You can use the DateTime.ToString method:

Dim strDisplayDate As String = dTheDate.ToString("dd/MM/yyy")
So that I can make some comparisons later on in my program using UK format.

To create a DateTime with a different culture, you can do the following:

'Create a CultureInfo for "English - United Kingdom"
Dim culture As New CultureInfo("en-GB", True)
Dim sUKDate As String = "28/04/2004"
Dim dUK As DateTime
dUK = DateTime.Parse(sUKDate,culture,DateTimeStyles.NoCurrentDateDefault)
Everytime I try this I get an error.

What error do you get?
 
* "Simon Osborn said:
I've have tried that approach but it will only work if I format the datetime
the same as what is set in the regional settings.

Either that or or I'm doing something drastically wrong.

Try to mask all the "/" with "\/".
 
I still get problems when comparing later on... i.e. using datediff.

What I don't want to have to do is worry about what the regional settings
are. I want to be able to format to what ever I want and if need be do a
datediff or anything along those lines.
 
* "Simon Osborn said:
I still get problems when comparing later on... i.e. using datediff.

What I don't want to have to do is worry about what the regional settings
are. I want to be able to format to what ever I want and if need be do a
datediff or anything along those lines.

Format doesn't have anything to do with 'DateDiff'. The format is only
used to get a "human readable" string representation of the 'DateTime''s
value. Post your code.
 
Back
Top