|
This function was created to compensate for the poor date and time formatting capabilities available to the ASP (VBScript) developer. FormatDateTime() just doesn't cut it! This function is analgous to the Format() function in the Visual Basic world. The function takes two parameters: - A valid date object
- A formatting string such as
'mm-dd-yyyy'
The first parameter can be a Variant Date object or an expression that evaluated to Variant Date object. The second parameter is a Variant String composed of the following date and time tokens: yy - year value, 2-digits yyyy - year value, 4-digits m - month value (1 to 12), not zero-padded mm - month value (1 to 12), zero-padded d - day value (1 to 31), not zero-padded dd - day value (1 to 31), zero-padded
h - hours value, not zero-padded hh - hours value, zero-padded m or mm - minutes value, always zero-padded s or ss - seconds value, always zero-padded
- forward slash(/)
- hyphen (-)
If you to use date and time formatting, then separate the date from the time with a space. Note: fmrDateTime requires a small utility function called ZeroPads to -- you guessed it -- zero-pad values. It is included below. <%
Dim dToday, dXmas2001
Response.Write "Today's date: " & fmtDateTime(Now(), "yyyy-mm-dd")
Response.Write "Yesterday's date: " & fmtDateTime(DateAdd("d", -1, Now()), "yyyy-mm-dd")
dXmas2001 = #12/25/2001 12:45:50#
Response.Write "Christmas 2001: : " & fmtDateTime(dXmas2001, "m/dd/yyyy hh:mm")
%>
<%
Function fmtDateTime(byval d, byval pat)
Dim Tokens, token, delim, i, date_part, time_part, DateTokens, TimeTokens
If IsNull(d) Then
fmtDateTime = ""
Exit Function
End If
If TypeName(d) <> "Date" Or Not IsDate(d) Then
fmtDateTime = "Invalid date parameter."
Exit Function
End If
If InStr(pat, " ") > 0 Then
Tokens = Split(pat, " ") ' should be 2 tokens --> (0)date (1)time
Else
Tokens = Split(pat, "") ' date OR time formatting
End If
bIsDate = False : bIsTime = False
For Each token In Tokens
If InStr(token, "-") > 0 Or InStr(token, "/") > 0 Then
If InStr(token, "-") Then
delim = "-"
ElseIf InStr(token, "/") Then
delim = "/"
End If
DateTokens = Split(token, delim)
For i = 0 To UBound(DateTokens)
Select Case CStr(DateTokens(i))
Case "yy"
DateTokens(i) = Right(CStr(DatePart("yyyy", d)), 2)
Case "yyyy"
DateTokens(i) = CStr(DatePart("yyyy", d))
Case "m"
DateTokens(i) = CStr(DatePart("m", d))
Case "mm"
DateTokens(i) = ZeroPad(CStr(DatePart("m", d)),2)
Case "d"
DateTokens(i) = CStr(DatePart("d", d))
Case "dd"
DateTokens(i) = ZeroPad(CStr(DatePart("d", d)),2)
Case Else
fmtDateTime = "Invalid date format : " & token
Exit Function
End Select
Next
date_part = Join(DateTokens, delim)
End If
If InStr(token, ":") > 0 Then
TimeTokens = Split(token, ":")
For i = 0 To UBound(TimeTokens)
Select Case CStr(TimeTokens(i))
Case "h"
TimeTokens(i) = Right(CStr(DatePart("h", d)), 2)
Case "hh"
TimeTokens(i) = ZeroPad(CStr(DatePart("h", d)),2)
Case "m", "mm" ' always zero-pad minutes
TimeTokens(i) = ZeroPad(CStr(DatePart("n", d)),2)
Case "s", "ss" ' always zero-pad seconds
TimeTokens(i) = ZeroPad(CStr(DatePart("s", d)),2)
Case Else
fmtDateTime = "Invalid time format : " & token
Exit Function
End Select
Next
time_part = Join(TimeTokens, ":")
End If
Next
fmtDateTime = Trim(date_part & " " & time_part)
End Function
Function ZeroPad(byval str, byval iSize)
ZeroPad = String((iSize - Len(str)), "0") & Trim(str)
End Function
%>
|
|
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By aspfree developerWorks - FREE Tools! | Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
| | | | Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
| | | | Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
| | | | Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
| | | | IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
| | | | This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
| | | | Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
| | | | Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |