The code snippet below displays the date with the ordinal suffix and the current time. On the Page_Load event the text property of the label 'lblDateTime' is set to the returned value of the Function ReturnDateTime. Figure 1 shows an example of how the date and time are formatted.
Figure 1<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Function ReturnDateTime() As String
Dim theDateTime As String = ""
Dim NumericDayofMonth As Integer = Date.Now.Day
Dim OrdinalSuffix As String = ""
Select Case NumericDayofMonth
Case 1, 21, 31
OrdinalSuffix = "st"
Case 2, 22
OrdinalSuffix = "nd"
Case 3, 23
OrdinalSuffix = "rd"
Case Else
OrdinalSuffix = "th"
End Select
Dim NumericMonthofYear As Integer = Date.Now.Month
Dim MonthofYear As String = ""
Select Case NumericMonthofYear
Case 1
MonthofYear = "Jan"
Case 2
MonthofYear = "Feb"
Case 3
MonthofYear = "Mar"
Case 4
MonthofYear = "Apr"
Case 5
MonthofYear = "May"
Case 6
MonthofYear = "Jun"
Case 7
MonthofYear = "Jul"
Case 8
MonthofYear = "Aug"
Case 9
MonthofYear = "Sep"
Case 10
MonthofYear = "Oct"
Case 11
MonthofYear = "Nov"
Case 12
MonthofYear = "Dec"
End Select
Dim NumericDayofWeek As Integer = Date.Now.DayOfWeek
Dim DayofWeek As String = ""
Select Case NumericDayofWeek
Case 1
DayofWeek = "Monday"
Case 2
DayofWeek = "Tuesday"
Case 3
DayofWeek = "Wednesday"
Case 4
DayofWeek = "Thursday"
Case 5
DayofWeek = "Friday"
Case 6
DayofWeek = "Saturday"
Case 7
DayofWeek = "Sunday"
End Select
theDateTime &= DayofWeek & " " 'Monday, Tuesday, Wednesday etc
theDateTime &= DateTime.Now.Day.ToString() '1, 2, 3, 4 etc
theDateTime &= OrdinalSuffix & " " 'st, nd, rd, th
theDateTime &= MonthofYear & " " 'Jan, Feb etc
theDateTime &= DateTime.Now.Year.ToString() & " " '2006, 2007 etc
theDateTime &= DateTime.Now.Hour.ToString() & ":" 'hour
theDateTime &= DateTime.Now.Minute.ToString() & ":" 'minute
theDateTime &= DateTime.Now.Second.ToString() 'seconds
Return theDateTime
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
lblDateTime.Text = ReturnDateTime()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Date and Time with Ordinal Suffix</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDateTime" runat="server" />
</div>
</form>
</body>
</html>
Get the best asp web hosting provider now and save 30%
Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.