I need to convert the date from mm/dd/yyyy to dd/mm/yyyy in vbscript
So far I have this: FormatDateTime(negativeItemsRS("ItemDate"), 0)
Its displaying the date in the format of mm/dd/yyyy. I want to convert that to a dd/mm/yyyy
Please help not sure how to do this.
Thank开发者_StackOverflows,
You have to set the locale id to one that uses the date format that you want. I don't remember which format used where, but either UK (2057) or US (1033) should work.
You haven't specified your environment. In ASP you could use the LCID property in the Language directive or in the Session or Response classes, depending on what scope you want for the setting:
<%@Language="VBScript" LCID="1033"%>
or
Session.LCID = 1033
or
Response.LCID = 1033
To change the date from MM/DD/YYY to DD/MM/YYYY in VB Script very simple steps can be used as given below
Let say "date1" (MM/DD/YY) = 03/06/2014 I want "date2" to be in DD/MM/YY as 06/03/2014
d = Day(date1)
m = Month(date1)
y = Year(date1)
date2 = d & "/" & m & "/" & y
Will give the required result.
What you need is a FormatDate function. I used to do this the hard way with manual concatentation, but I discovered that there are a few .NET libraries that are accessible from COM and thus from ASP Classic. My version leverages the fact that I have a StringBuilder class which is a wrapper around the StringBuilder class in .NET.
'******************************************************************************
Public Function FormatDate( sFormat, dDateValue )
'PURPOSE: To format a date with any arbitrary format
'ARGS:
' sFormat is the defined formats as used by the .NET Framework's System.DateTimeFormatInfo.
' Note that this format is case-sensitive.
'CALLS:
' 1. System.Text.StringBuilder class in the .NET Framework.
'EXAMPLE CALL:
' Dim sFormatedDate
' sFormatedDate = FormatDate( "MM/dd/yy", "1/1/1900 12:00 AM" )
' Or
' sFormatedDate = FormatDate( "MM/dd/yyyy", "1/1/1900 12:00 AM" )
'DESIGN NOTE:
' This function leverages the fact that System.Text.StringBuilder is COMVisible.
' Thus, we can call its AppendFormat function from here.
' You can find more information about the format string parameters allowed at
' http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
Dim oStringBuilder
Dim sSbFormatString
Dim dDate
If Not IsDate( dDateValue ) Then
FormatDate = vbNullString
Exit Function
End If
On Error Resume Next
dDate = CDate(dDateValue)
If Err.number <> 0 Then
FormatDate = vbNullString
Exit Function
End If
'if an empty format string is passed, then simply return
'the value using the default shortdate format.
If Len(sFormat & vbNullString) = 0 Then
sSbFormatString = "{0:d}"
Else
sSbFormatString = "{0:" & sFormat & "}"
End If
Set oStringBuilder = CreateObject("System.Text.StringBuilder")
Call oStringBuilder.AppendFormat(sSbFormatString, dDate)
FormatDate = oStringBuilder.ToString()
Set oStringBuilder = Nothing
End Function
'**************************************************************************
' Use this class to concatenate strings in a much more
' efficient manner than simply concatenating a string
' (strVariable = strVariable & "your new string")
Class StringBuilder
'PURPOSE: this class is designed to allow for more efficient string
' concatenation.
'DESIGN NOTES:
' Originally, this class built an array and used Join in the ToString
' method. However, I later discovered that the System.Text.StringBuilder
' class in the .NET Framework is COMVisible. That means we can simply use
' it and all of its efficiencies rather than having to deal with
' VBScript and its limitations.
Private oStringBuilder
Private Sub Class_Initialize()
Set oStringBuilder = CreateObject("System.Text.StringBuilder")
End Sub
Private Sub Class_Terminate( )
Set oStringBuilder = Nothing
End Sub
Public Sub InitializeCapacity(ByVal capacity)
On Error Resume Next
Dim iCapacity
iCapacity = CInt(capacity)
If Err.number <> 0 Then Exit Sub
oStringBuilder.Capacity = iCapacity
End Sub
Public Sub Clear()
Call Class_Initialize()
End Sub
Public Sub Append(ByVal strValue)
Call AppendFormat("{0}", strValue)
End Sub
Public Sub AppendFormat(ByVal strFormatString, ByVal strValue)
Call oStringBuilder.AppendFormat(strFormatString, (strValue & vbNullString))
End Sub
'Appends the string with a trailing CrLf
Public Sub AppendLine(ByVal strValue)
Call Append(strValue)
Call Append(vbCrLf)
End Sub
Public Property Get Length()
Length = oStringBuilder.Length
End Property
Public Property Let Length( iLength )
On Error Resume Next
oStringBuilder.Length = CInt(iLength)
End Property
'Concatenate the strings by simply joining your array
'of strings and adding no separator between elements.
Public Function ToString()
ToString = oStringBuilder.ToString()
End Function
End Class
So, with this class you could do something like:
FormatDate("dd/MM/yyyy", RS("DateField"))
Note that the string passed in is case-sensitive.
EDIT I see that at some point I amended my FormatDate function to eliminate the use of my VBScript StringBuilder class and instead just use the .NET class directly. I'll leave the VBScript StringBuilder class in there for reference in case anyone is interested. (I did swap the order of the two however to make the code that appears at the top more applicable to the problem).
精彩评论