INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- 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.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."
Geography
Where in the world do Tek-Tips members come from?
|
Microsoft: Active Server Pages (ASP) FAQ
|
ASP 101
|
Formatting the Date and Time in ASP
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: Active Server Pages (ASP) FAQ Index
Back to Microsoft: Active Server Pages (ASP) Forum
My FAQ Archive
Email This FAQ To A Friend |
|
 |
|