BrainDump
  Home arrow BrainDump arrow Page 2 - VBScript Date Functions
Iron Speed
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
SunQuest
 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: 5 stars5 stars5 stars5 stars5 stars / 3
    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
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

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

    BRAINDUMP ARTICLES

    - Codes and Packages in Microsoft Project 2007
    - Windows 7: Rumors and Demos
    - XP SP3 Why Me?
    - Breaking Up Your Work in Microsoft Project
    - Breaking Work into Task-Sized Chunks
    - Putting Microsoft`s Worldwide Telescope Unde...
    - Handling Multiple Contracts with Indigo
    - Cleaning Out Your Data in XP
    - Multiple Service Contracts and Indigo
    - Cleaning Out Your Programs in XP
    - Handling Metadata with Indigo
    - Building Blocks for a WCF Service Web Site
    - Help! I Need Some Remote Assistance
    - Using Service Templates with Indigo
    - Windows XP Tips for Task Manager





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway