INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

ASP 101

Formatting the Date and Time in ASP by onpnt
Posted: 17 Feb 03 (Edited 27 Jun 03)

red 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.

(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)


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

and
Date 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:

Session.LCID = 0413
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:
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>"


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
Response.Write FormatDateTime(curDate, vbLongDate) & " " & FormatDateTime(curDate, vbLongTime)
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:
DateToday = Year(Date()) & ", " & MonthName(Month(date())) & ", " & Day(Date())
response.Write DateToday

output
2003, February, 17

or
DateToday = MonthName(Month(date())) &  ", " &  Day(Date()) & ", " & Year(Date())
response.Write DateToday

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
response.Write MonthName(Month(date), true) & "," & WeekdayName(weekday(date), true) & "," &  Year(Date())

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

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close