logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
European Travel
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel


dailyclick
All times in EST

Full Schedule
g
g JavaScript / Java Site

BellaOnline's JavaScript / Java Editor

g

Introduction to the JavaScript Date Object

Guest Author - Julie L Baumler

If you aren't at all familiar with Objects and Methods, you may want to start with my article document.write("Hello World") and other cases of something-dot-somethingelse.

One of the most useful JavaScript objects, particularly for beginners or people looking to add some simple context sensitivity to their webpages is the Date() object. The Date() object is a native built-in object, which means that it is available by default in all JavaScript implementations. You can count on it being there and you don't have to tell your program that you are going to use it.

The simplest thing to do with the date object is just use it to display the current date in the default format.

document.write(Date()) gives

This isn't necessarily how you'd want to format the date on a letter to your mom, but it works well for putting a date in the footer of a web page or dating your latest news update in fact, if you look around on the web, it's not uncommon to see dates in this form. (The exact format will vary depending on the locale settings on the computer where this is run.) It's an easy way to make your page always look current (as long as you make sure that the content that goes with it isn't obviously outdated!)

To do more with dates, you need to create your own instance of the date object. For instance, var now = new Date() to set it to the current date and time or var picoBarkday = new Date("December 20, 1999") to set it to a specific date. (Note: both now and picoBarkday are variables I chose.) Once we have our own date object, we can use different Date methods to change or find information about the date.

For the following examples, we set Pico's birthday as follows:
var picoBarkday = new Date("December 20 1999")

methodexampleresultNote
getDay()picoBarkday.getDay()1Day of Week, Sunday is day 0
getDate()picoBarkday.getDate()20Day of Month
getMonth()picoBarkday.getMonth()11Programmers count from 0, so this is December
getFullYear()picoBarkday.getFullYear()19994 Digit Year
getTime()picoBarkday.getTime()e.g. 945673200000JavaScript actually keeps track of time in Milliseconds since January 1st, 1970, this is that number


You can see these examples live and get the code here
This site needs an editor - click to learn more!

Add Introduction+to+the+JavaScript+Date+Object to Twitter Add Introduction+to+the+JavaScript+Date+Object to Facebook Add Introduction+to+the+JavaScript+Date+Object to MySpace Add Introduction+to+the+JavaScript+Date+Object to Del.icio.us Digg Introduction+to+the+JavaScript+Date+Object Add Introduction+to+the+JavaScript+Date+Object to Yahoo My Web Add Introduction+to+the+JavaScript+Date+Object to Google Bookmarks Add Introduction+to+the+JavaScript+Date+Object to Stumbleupon Add Introduction+to+the+JavaScript+Date+Object to Reddit




Basic Intro to Objects and Methods
A First JavaScript Program
JavaScript/Java Newsletter
RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map


For FREE email updates, subscribe to the JavaScript / Java Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


g features
Hearing Dog Tool - Adding Randomness

Hearing Dog Training Tool setTimeout()

GCJ - an Independant Java Implementation

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor