BrainDump
  Home arrow BrainDump arrow Page 2 - VBScript Date Functions
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

VBScript Date Functions
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-01-07

    Table of Contents:
  • VBScript Date Functions
  • Functioning it Up with CDate
  • Working with the Date Function
  • The Vainglorious DateDiff Function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    VBScript Date Functions - Functioning it Up with CDate


    (Page 2 of 4 )

    The CDate function allows the user to check if a value can be converted to a date or time. It can also be used to do the actual conversion. Here is an example that converts a string to a date:


    <html>

    <body>

    <script type="text/vbscript">

    birthdate="April 22 1977"

    if IsDate(birthdate) then

    document.write(CDate(birthdate))

    end if

    </script>

    </body>

    </html>

    The result of this program would be:

      4/22/1977

    Note that in the above example the valid date could be converted. But what if it couldn't? Try changing the code to show birthdate= "Afrail 10 2BD0." You will notice that there is no output; that is because the value cannot be converted to a date. Ideally, we would want to let the user know that the value is invalid. Here is how we would do so:


    <html>

    <body>


    <script type="text/vbscript">

    birthdate="Afrail 22 1977"

    if IsDate(birthdate) then

    document.write(CDate(birthdate))

    else

    document.write("That value cannot be converted")

    document.write("<p>The pain train is coming. Whoo! Whoo!</p>")

    end if

    </script>

    </body>

    </html>

    Because the value is invalid, and we added an else clause, this results in the text:

      That value cannot be converted

      The pain train is coming. Whoo! Whoo!

    You can also convert numbers to a date, like so:


    <html>

    <body>

    <script type="text/vbscript">

    releasedate=#12/22/2007#

    if IsDate(releasedate) then

    document.write(CDate(releasedate))

    end if

    </script>

    </body>

    </html>

    Or


    <html>

    <body>

    <script type="text/vbscript">

    releasedate=#12 22 2007#

    if IsDate(releasedate) then

    document.write(CDate(releasedate))

    end if

    </script>

    </body>

    </html>

    And lastly, you can convert a value to time in the following manner:


    <html>

    <body>

    <script type="text/vbscript">

    witchinghour="12:00:00 AM"

    if IsDate(witchinghour) then

    document.write(CDate(witchinghour))

    else

    document.write("That isn't the witching hour you fool!")

    end if

    </script>

    </body>

    </html>

    More BrainDump Articles
    More By James Payne


       · Thanks for stopping by. In this article we discuss Date Functions in VBScript, like...
       · Need a printer-friendly version of the page!
     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT