Sams Teach Yourself JavaScript in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Part I: Getting Started
- Hour 1. Understanding JavaScript
- Hour 2. Creating a Simple Script
- Hour 3. How JavaScript Programs Work
- Part II: Learning JavaScript Basics
- Hour 4. Using Functions and Variables
- Hour 5. Using Strings and Arrays
- Hour 6. Testing and Comparing Values
- Hour 7. Repeating Yourself: Using Loops
- Hour 8. Using Math and Date Functions
- Part III: The Document Object Model (DOM)
- Hour 9. Working with the Document Object Model
- Hour 10. Responding to Events
- Hour 11. Using Windows and Frames
- Hour 12. Getting Data with Forms
- Hour 13. Using Graphics and Animation
- Part IV: Moving on to Advanced JavaScript Features
- Hour 14. Creating Cross-Browser Scripts
- Hour 15. Creating Custom Objects
- Hour 16. Working with Sounds and Plug-Ins
- Hour 17. Debugging JavaScript Applications
- Part V: Working with Dynamic HTML (DHTML)
- Hour 18. Working with Style Sheets
- Hour 19. Using Dynamic HTML (DHTML)
- Hour 20. Using Advanced DOM Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Hour 23. Creating DHTML Applications
- Hour 24. JavaScript Tips and Tricks
- Part VII: Appendices
- Appendix A. Other JavaScript Resources
- Appendix B. Tools for JavaScript Developers
- Appendix C. Glossary
- Appendix D. JavaScript Quick Reference
- Appendix E. DOM Quick Reference
Adding JavaScript Statements
To complete your script, you will need to determine what the local and UTC times are, and then display them to the browser. Fortunately all of the hard parts, such as converting between dates, are built into the JavaScript interpreter.
Storing Data in Variables
To begin the script, you will use a variable to store the current date. You will learn more about variables in Hour 4, "Using Functions and Variables." For now, think of them as containers that can hold something—a number, or in this case, a date.
To start writing the script, add the following line after the first <script> tag. Be sure to use the same combination of capital and lowercase letters in your version, since JavaScript commands and variable names are case-sensitive.
now = new Date();
This statement creates a variable called now and stores the current date and time in it. This statement and the others you will use in this script use JavaScript's built-in Date object, which allows you to conveniently handle dates and times. You'll learn more about working with dates in Hour 8, "Using Math and Date functions."
Calculating the Results
Internally, JavaScript stores dates as the number of milliseconds since January 1, 1970. Fortunately, JavaScript includes a number of functions to convert dates and times in various ways, so you don't have to figure out how to convert milliseconds to day, date, and time.
To continue your script, add the following two statements before the final </script> tag:
localtime = now.toString(); utctime = now.toGMTString();
These statements create two new variables: localtime, containing the current time and date in a nice readable format, and utctime, containing the UTC equivalent.
Creating Output | Next Section

Account Sign In
View your cart