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 Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact Advertise on Bucaro TecHelp Advertise Here RSS News Feeds News Feeds

HTML5 Solutions: Essential Techniques for HTML5 Developers

Essential Techniques for HTML5 Developers

HTML5 brings the biggest changes to HTML in years. Web designers now have new techniques, from displaying video and audio natively in HTML, to creating realtime graphics on a web page without a plugin.

This book provides a collection of solutions to all of the most common HTML5 problems. Every solution contains sample code that is production-ready and can be applied to any project.

Click Here

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.

RSS Feed RSS Feed



Web Design Sections

Eloquent JavaScript: A Modern Introduction to Programming

Eloquent JavaScript

Eloquent JavaScript Eloquent JavaScript goes beyond the cut-and-paste scripts of the recipe books and teaches you to write code that's elegant and effective. You'll start with the basics of programming, and learn to use variables, control structures, functions, and data structures. Then you'll dive into the real JavaScript artistry: higher-order functions, closures, and object-oriented programming.

Reader Anthony says, "This book is not your typical Javascript book. Others have a utilitarian approach. In stark contrast, Eloquent JavaScript does not merely provide you a checklist of things to learn but rather paints a panorama of the possibilities that programming provides. Javascript is merely the tool used to introduce these to the reader.

Click here for more information.


[Site User Agreement] [Advertise on This site] [Search This Site] [Contact Form]
Copyright©2001-2011 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268