//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Date/Time Routines :: Can I make VBScript format dates for me?


Can I make VBScript format dates for me?

Well, VBScript's FormatDateTime leaves a lot to be desired. There really are only two ways to format a date by itself: VBLongDate and VBShortDate. Below are a few functions that will help you get dates into other relatively common formats. Many of these functions use the following function, from Article #2300
 
<% 
    Function pd(n, totalDigits) 
        if totalDigits > len(n) then 
            pd = String(totalDigits-len(n),"0") & n 
        else 
            pd = n 
        end if 
    End Function 
%>
 
YYYYMMDD 
 
(This is the preferred date format for passing dates to a SQL Server database, as it eliminates the need to worry about regional settings. See Article #2260 for more information.) 
 
<% 
    response.write YEAR(Date()) & _ 
        Pd(Month(date()),2) & _ 
        Pd(DAY(date()),2) 
%>
 
YYYY-MM-DD 
 
(This is the only safe date format to pass to Access.) 
<% 
    response.write YEAR(Date()) & _ 
        "-" & Pd(Month(date()),2) & _ 
        "-" & Pd(DAY(date()),2) 
%>
 
DDMMYYYY 
 
<% 
    response.write Pd(DAY(date()),2) & _ 
        Pd(Month(date()),2) & _ 
        YEAR(Date()) 
%>
 
MMDDYYYY 
 
<% 
    response.write Pd(Month(date()),2) & _ 
        Pd(DAY(date()),2) & _ 
        YEAR(Date()) 
%>
 
DD-MM-YY 
 
<% 
    response.write pd(DAY(date()),2) & "-" & _ 
        pd(MONTH(date()),2) & "-" & _ 
        pd(RIGHT(YEAR(date()),2),2) 
%>
 
YY-MM-DD 
 
<% 
    response.write pd(RIGHT(YEAR(date()),2),2) & "-" & _ 
        pd(MONTH(date()),2) & "-" & _ 
        pd(DAY(date()),2) 
%>
 
MM/DD 
 
<% 
    response.write pd(MONTH(date()),2) & "/" & _ 
        pd(DAY(date()),2) 
%>
 
Month D 
 
<% 
    response.write MonthName(Month(date())) & _ 
        " " & DAY(date()) 
%>
 
DD Mon YY 
 
<% 
    response.write pd(DAY(date()),2) & " " & _ 
        MonthName(Month(date()),true) & _ 
        " " & RIGHT(YEAR(date()),2) 
%>
 
Weekday, Month D 
 
<% 
    response.write WeekdayName(Weekday(Date())) & _ 
        ", " & MonthName(Month(date())) & _ 
        " " & DAY(date()) 
%>
 
Please let us know if there are any other date formats you'd like to see. 
 

 
Replicating VB's Format() function 
 
Thanks to Chris Hohmann, who pointed out a script by Scott Dixon which provided the basis for the following: 
 
<% 
    Function Format(vExpression, sFormat) 
 
        set fmt = CreateObject("MSSTDFMT.StdDataFormat") 
        fmt.Format = sFormat 
 
        set rs = CreateObject("ADODB.Recordset") 
        rs.Fields.Append "fldExpression", 12 ' adVariant 
 
        rs.Open 
        rs.AddNew 
 
        set rs("fldExpression").DataFormat = fmt 
        rs("fldExpression").Value = vExpression 
 
        Format = rs("fldExpression").Value 
 
        rs.close: Set rs = Nothing: Set fmt = Nothing 
 
    End Function 
 
    ' sample usage: 
 
    response.write format(now(), "yyyy-mm-dd") 
    response.write "<br>" 
    response.write format(time(), "hh:mm") 
%>
 
The fmt object depends on the availability of MSSTDFMT.dll, which is installed by Visual Studio 6.0. I have not tried to run this script on a machine that did not have Visual Studio 6.0 or later installed, so I'm not sure it will work if you simply copy and/or register the DLL on the target machine. 
 
You can also see this date formatting class for other ideas.

Related Articles

Can I get millisecond accuracy in ASP?
Could I get a little help with dates?
Given a date, how do I find the beginning and end of that week?
Given two dates, how do I determine an age?
How do I calculate dates, such as the first day of the month?
How do I convert a DATEDIFF to days, hours, and minutes?
How do I convert a timespan, in seconds, to HH:MM:SS?
How do I convert local time to UTC (GMT) time?
How do I count the number of business days between two dates?
How do I delimit/format dates for database entry?
How do I determine the number of seconds since 1/1/1970?
How do I display time in military format?
How do I implement a calendar / datepicker in ASP?
How do I select time only from a DATETIME column?
Why do I have problems inserting NOW() into a database?
Why does JavaScript's document.lastModified() not work in ASP files?

 

 


Created: 7/2/2002 | Last Updated: 3/24/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (366)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...