将日期时间格式更改为英国标准

时间:2013-09-02 07:20:08

标签: datetime asp-classic vbscript

我想更改经典asp中的默认日期时间格式。我更改了区域和语言控制面板中的英国格式。

我用以下代码测试过。

Response.Write Now()

它正确显示英国时间格式(“dd / mm / yyyy”)。但我改变了下面的代码。

Response.Write CStr(Now())

它显示美国时间格式,如“mm / dd / yyyy”。

如何将其修复为英国时间格式?感谢。

3 个答案:

答案 0 :(得分:2)

听起来IIS上的区域设置设置为美国 - 将其设置为英国。这将为您提供免代码修复:)

另外,请看一下类似的问题:Change Default Locale in IIS 6.0

答案 1 :(得分:1)

MSDN文档here说:

  

CStr功能使用系统的区域设置来确定如何执行转换   ...
  包含系统短日期格式的日期的字符串

因此,请转到服务器控制面板中的“区域”设置,并更改其中的日期格式。

如果你想要一个更强大的解决方案,那么你需要自己从日期部分组装字符串,或者远离传统的ASP。

此问题中显示了一些解决方案:How can I reformat a date stored as a string in VB?

答案 2 :(得分:0)

这里有两种主要的方法(假设你的当前值是一个字符串)。

方法一:拆分日期并重新格式化

此方法涉及使用String.Split()方法,该方法将创建一个提供字符串和分隔符的数组。通过引用数组的索引,您可以将它们重建为您想要的格式:

'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))

方法二:解析和格式化

第二种方法将使用DateTime.ParseExact()解析您的字符串,并允许您指定日期最初的格式,然后您可以使用ToString()方法根据需要输出它:

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")
相关问题