Date Formatting in Classic ASP

In a previous post we showed you how to properly format date for a specific culture in ASP. In this article I will show you how to do the same but in Classic ASPGetting Started

Date formatting is limited in Classic ASP by default. However there is an specific function that will allow you to format any date in several different ways based on your server’s regional configuration. This function in Classic ASP is “FormatDateTime()”.

The FormatDateTime() offers you other functions that you can use to control how to display the date formatting. These are:

Year(date): This one returns a whole number representing the year.

Month (date): Returns a number (1-12), representing the month of the year.

Day(date): Returns a number (1-31), representing the day of the month.

Hour(time): Returns a number (0-23), representing the hour of the day.

Minute(time): Returns a number (0-59), representing a minute in a day.

Second(time): Returns a number (0-59), representing seconds.

The above functions when called, return a number. To display and format an specific date, you need to do it like this:

'Function used to pad numeric values, i.e 1 to 01
'zeros - Number of zero's to prefix the value with.
Function format_zeros(value, zeros)
  If Len(value) < zeros Then
    Do Until Len(value) = zeros
      value = "0" & value
  End If
  format_zeros = value
End Function

Dim somedate, sometime, somedatetimestamp

'Build the date string in the format yyyy-mm-dd
somedate = Year(Date()) & "-" & format_zeros(Month(Date()), 2) & "-" & format_zeros(Day(Date()), 2)
'Build the time string in the format hh:mm:ss
sometime = format_zeros(Hour(Time()), 2) & ":" & format_zeros(Minute(Time()), 2) & ":" & format_zeros(Second(Time()), 2)
'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss
somedatetimestamp = somedate & " " & sometime

Call Response.Write(somedatetimestamp)

The above code not only will display an specific date, but it will format it in a way that will stripe all zeros, which if not done correctly could cause problems if your application relies on dates.

This Concludes Date Formatting in Classic ASP

Looking for quality Windows Hosting? Look no further than Arvixe Web Hosting!

Happy Hosting!

Tags: , , , , , , , , , , | Posted under ASP .NET 2, Windows Server Admin | RSS 2.0

Author Spotlight

Rodolfo Hernandez

I like photography and reading books. Currently working for Arvixe as Elgg Community Liaison. Elgg Security Expert Web Security Expert CEO of UDP SW Social Web

Leave a Reply

Your email address will not be published. Required fields are marked *