zanhtet
zanhtet

Reputation: 2050

Change datetime format to UK standard

I would like to change default datetime format in classic asp. I changed the UK format in Region and Language of Control Panel.

I tested with below code.

Response.Write Now()

It shows UK time format correctly("dd/mm/yyyy"). But I changed my code below.

Response.Write CStr(Now())

It shows US time format like that "mm/dd/yyyy".

How can I fix it to UK time format? Thanks.

Upvotes: 0

Views: 1950

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

The MSDN documentation here says:

The CStr function uses the locale setting of your system to determine how to perform conversions.
...
A String containing a date in the short-date format of your system.

So go to the "Region" settings in the control panel of your server and change the date format there.

If you want a more robust solution, then you'll need to assemble the string yourself from date parts, or move away from classic ASP.

There are some solutions shown in this question: How can I reformat a date stored as a string in VB?

Upvotes: 1

Chris
Chris

Reputation: 2952

It sounds like the regional settings on IIS is set to United States - set it to UK. This will give you a code free fix :)

Also, have a look at this similar issue: Change Default Locale in IIS 6.0

Upvotes: 2

Albert Laure
Albert Laure

Reputation: 1722

There are two main approaches that you can take here (assuming that your current value is a string).

Method One : Split the Date and Reformat

This method involves using the String.Split() method which will create an array provided a string and a delimiter. By referencing the indices of the array you can reconsitute them into the format you desire :

'Your Date Components being split into an array'
Dim dc() As String = rs("StayDate").Split("/"c)

'Creates a new string by moving around the different indices 0 - dd, 1 - MM, 2 - yyyy'
Dim newDate As String = String.Format("{0}/{1}/{2}", dc(1), dc(0), dc(2))

Method Two : Parsing and Formatting

The second method will parse your string using DateTime.ParseExact() and will allow you to specify the format that you date is in initially and then you can use the ToString() method to output it as you see fit :

Imports System.Globalization

'Parses the date and outputs it in your new format'
Dim newDate As String = DateTime.ParseExact(rs("StayDate").Value, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy")

Upvotes: 0

Related Questions