Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Private Function FormatDate(ByVal dDate As Date) As String
Dim sRet As String = dDate.ToLongDateString
Dim sParts As String() = sRet.Split(",")
'turn 01 into just 1
sParts(1) = sParts(1).Substring(0, sParts(1).Length - 2) & CInt(sParts(1).Substring(sParts(1).Length - 2, 2))
If sParts(1).Substring(sParts(1).Length - 3, 1) = "1" Then
'teens
sParts(1) += "th"
Else
Select Case sParts(1).Substring(sParts(1).Length - 2)
Case "1"
sParts(1) += "st"
Case "2"
sParts(1) += "nd"
Case "3"
sParts(1) += "rd"
Case Else
sParts(1) += "th"
End Select
End If
sRet = sParts(1) & sParts(2)
Return sRet
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write(FormatDate(System.DateTime.Now))
End Sub
Private Function FormatDate(ByVal dDate As Date) As String
Dim sRet As String = dDate.ToLongDateString
Dim sParts As String() = sRet.Split(",")
'turn 01 into just 1
[!]sParts(1) = sParts(1).Substring(0, sParts(1).Length - 2) & CInt(sParts(1).Substring(sParts(1).Length - 2, 2))[/!]
If sParts(1).Substring(sParts(1).Length - 3, 1) = "1" Then
'teens
sParts(1) += "th"
Else
Select Case sParts(1).Substring(sParts(1).Length - 2)
Case "1"
sParts(1) += "st"
Case "2"
sParts(1) += "nd"
Case "3"
sParts(1) += "rd"
Case Else
sParts(1) += "th"
End Select
End If
sRet = sParts(1) & sParts(2)
Return sRet
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sb As New StringBuilder
sb.Append(System.DateTime.Now.ToString("MMMM"))
sb.Append(" " & GetOrdinal(System.DateTime.Now))
sb.Append(" " & System.DateTime.Now.Year)
Response.Write(sb.ToString)
End Sub
Private Function GetOrdinal(ByVal CurrentDate As Date) As String
Select Case CurrentDate.Day
Case 1, 21, 31
Return CurrentDate.Day & "st"
Case 2, 22
Return CurrentDate.Day & "nd"
Case 3, 23
Return CurrentDate.Day & "rd"
Case Else
Return CurrentDate.Day & "th"
End Select
End Function
It could be the time zones that are different? When I look at my date that is passed into the function, it appears in the format:Hmmm... I don't get an exception with System.DateTime.Now.
#5/9/2007 5:20:08 PM#
#5/9/2007 11:32:48 AM#
"Wednesday, May 09, 2007"
09 May 2007
Yes, that's correct.Are you using VS 2005 with Framework version 2.0 like I am?