65.9K
CodeProject is changing. Read more.
Home

Displaying the server's current date

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.29/5 (4 votes)

Feb 15, 2000

CPOL
viewsIcon

90473

A simple routine to display the current date of the server in the clients browser

Here's a simple function to display the date on the server, either in the form '7 Feb 2002' or 'Thursday 7th February, 2002' depending on whether Abbreviate is True or False.

' DateVal is the date to display

' If Abbreviate is True then the date is displayed in short form

' Otherwise it's displayed verbose

Function DateString(DateVal, Abbreviate)
Dim intDate, strDay, strMonth, strYear

    intDate  = Day(DateVal)
    strYear  = Year(DateVal)
    
    if Abbreviate Then
        strMonth = MonthName(Month(DateVal), True)
        DateString = intDate & " " & MonthName(Month(DateVal), True) & " " & strYear
    Else
        strMonth = MonthName(Month(DateVal))
        strDay   = WeekDayName(WeekDay(DateVal), False, vbSunday)

        Dim suffix
        suffix   = "th"
        Select Case intDate
            case 1,21,31 : suffix = "st"
            case 2,22    : suffix = "nd"
            case 3,23    : suffix = "rd"
        End Select

        DateString  = strDay & " " & intDate & suffix & " " & strMonth & ", " & strYear
    End If
End Function

To use this in an ASP page just include the above script, then add the following where you wish to display the date:

<p>Today is <%= DateString(Date(), False) %></p>