When you want to work with a date in Java Script, you need to use the Date object. In this article, you learn how to create a Date object for today, or for any date in the past or future. You learned how to find the difference between two dates, and how to format a date string any way that you desire. You also learned how to validate a date string returned from a text box in a form, which involved an introduction to JavaScript regular expressions.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
Your Ad Here!
Your ad here for one full year, only $20. Send me your ad copy, up to six lines, I'll post it ASAP. After you inspect your ad online, you can pay through PayPal.
Click here for more information.

Using the Java Script Date Object

When you want to work with a date in Java Script, you need to create a Date object. The code shown below creates a Date object and uses it to display today's date in a message box.

var today = new Date();
alert(today);

- If you want to follow along and get some hands-on experience, create a new file in Windows Notepad or your favorite ASCII text editor and type in the html code shown below.

<html>
<body>
<script type="text/javascript">

... JavaScript code here ...

</script>
</body>
</html>

Save the file with the extension .htm. For each example, replace the text "... JavaScript code here ..." with the example code. Resave the file, then double-click on the file name to execute the example.

If you're using Internet Explorer, you'll receive the message "Internet Explorer has restricted this webpage from running scripts". Right-click on the message and in the popup menu that appears, select "Allow Blocked Content...". This is how Microsoft Expects to protect Windows, by making it annoying and unusable.

The Date object constructor reads the computers internal clock. The internal value in a Date object is the number of milliseconds since midnight January 1, 1970 in GMT (Greenwich Mean Time).

- In the old days, we used to call this "Greenwich Meridian Time", because it's the time at Earth's 0 degrees longitude line, which happens to pass through the city of Greenwich, England, where they have the over 325 year old Greenwich Observatory. But apparently some people couldn't figure out what a "Meridian" was, so they changed it to "Mean". Today, most people use UTC (Coordinated Universal Time), which is GMT based on an atomic clock and expressed as a 24 hour clock.

The date in the Date object is a static date, it will not change over time, you need to update it programmatically. When you display the contents of a Date object, JavaScript converts it to the local time zone as configured in your PC's Control Panel.

Getting Past or Future Dates

You can get a date from the past or future by passing a date, formatted as yyyy,mm,dd to the Date object, as shown below.

var pastDate = new Date(2007,11,18);
alert(pastDate);

- Note: The Date object uses a zero-based month, with January being month 0. In other words, subtract 1 from the number of the month that you pass to the Date object.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Slide Show Code
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Web Designer's Reference
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using Java Script
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269