The simplest thing to do with the date object is just use it to display the current date in the default format.
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")
|getDay()||1||Day of Week, Sunday is day 0|
|getDate()||20||Day of Month|
|getMonth()||11||Programmers count from 0, so this is December|
|getFullYear()||1999||4 Digit Year|
You can see these examples live and get the code here
This site needs an editor - click to learn more!
You Should Also Read:
Basic Intro to Objects and Methods
Editor's Picks Articles
Top Ten Articles
Content copyright © 2022 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.