Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting the Date and Time in ASP

ASP 101

Formatting the Date and Time in ASP

by  onpnt  Posted    (Edited  )
[color red]red[/color] denotes all working code examples

Date and Time formatting have no restrictions when outputting them to the screen in ASP
Although the question arising often of how to format the date/time the way you want to.
This FAQ is going to be a quick reference to how you can accomplish the date/time
formatting the way you need it to appear.
[color blue]
(Remember that database functionality is compromised when formatting the date
time while performing insert's and select's. Be certain you format it correctly
when performing database functions)
[/color]


of course we need to know the basics, which are
[color red]Now[/color] which outputs = 2/17/2003 1:28:18 PM

and
[color red]Date[/color] which outputs = 2/17/2003

First method is to use the Locale ID (LCID) session property.
This method overrides the web server's default settings for the date time formatting
example:

[color red]Session.LCID = 0413[/color]
results in dd/mm/yyyy format

there is a great table and further example of this method here
http://www.webwizguide.info/asp/faq/date_time_settings.asp

second method is the FormatDateTime function
many VBers will know this function well, but overlooked many times in vbscript/ASP
example:
[color red]Dim curDate
curDate = Now()
Response.Write "vbGeneralDate - " & FormatDateTime(curDate, vbGeneralDate) & "<BR>"
Response.Write "vbLongDate - " & FormatDateTime(curDate, vbLongDate) & "<BR>"
Response.Write "vbShortDate - " & FormatDateTime(curDate, vbShortDate) & "<BR>"
Response.Write "vbLongTime - " & FormatDateTime(curDate, vbLongTime) & "<BR>"
Response.Write "vbShortTime - " & FormatDateTime(curDate, vbShortTime) & "<BR>"
[/color]

output
vbGeneralDate - 2/17/03 12:12:38 PM
vbLongDate - Monday, February 17, 2003
vbShortDate - 2/17/03
vbLongTime - 12:12:38 PM
vbShortTime - 12:12

take it further and do things like
[color red]Response.Write FormatDateTime(curDate, vbLongDate) & " " & FormatDateTime(curDate, vbLongTime)[/color]
outputing = Monday, February 17, 2003 12:29:28 PM


now that we have the basic formatting down with those two methods. lets talk about customizing it further.
you have the capabilities to format the date/time in any way you want. literally! the commonly used function
for this task are going to be Date(), Month(), MonthName(), Day(), Year(), Weekday(), WeekdayName()
with these built in functions we can do any format with the date we need or may want.
example:[color red]
DateToday = Year(Date()) & ", " & MonthName(Month(date())) & ", " & Day(Date())
response.Write DateToday [/color]
output
2003, February, 17

or[color red]
DateToday = MonthName(Month(date())) & ", " & Day(Date()) & ", " & Year(Date())
response.Write DateToday[/color]
output
February, 17, 2003

another addition is the abbreviation in WeekDayName as
WeekdayName(weekday(date), true) outputing = Mon
and the abbreviation of the month as
MonthName(Month(date), true) outputing = Feb

then we can do things like[color red]
response.Write MonthName(Month(date), true) & "," & WeekdayName(weekday(date), true) & "," & Year(Date())[/color]
outputing = Feb,Mon,2003

as you can see the limits are endless in however you want to format it.
if you are looking for a dynamic time to be printed to the screen then you are looking
in the wrong place. javascript is the client side scripting language to use there.

seeing as there was some confusion on what you can do with this information in how you can format the date in anyway you want
I decided to add this little section. The right(), left() Day(), Month(), Year() etc... functions are going to give you all kinds of options to format the output anyway you want.
eg:
Dim MyYear
MyYear = Month(Now()) & "/" & Day(Now()) & "/" & right(Year(Now()),2)
MyYear = cDate(MyYear)
Response.write MyYear 'output dd/mm/yy

alright then! Now you have a short example of how and what direction you need to use this information to work for you. happy coding!!! onpnt
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top