The Date object is used to set and to get certain time values that you can use in your JS scripts.
There are two important things you should know before using this object:
          1. The initial reference date is 1.1.1970, you can not work with its previous data.
          2. When you create a Date object, it uses the client computer (the visitor) date and time .
To use this object, you need to create an instance of the Date object, with the new keyword, as shown in the following example:

var instance_name = new Date();
- "instance_name" can be any variable name.
- This instruction stores the current date in the "instance_name" variable, as shown in the result of the following example.
<script type="text/javascript"><!--
var right_now = new Date();
document.write(right_now);        // Sun Jan 30 2011 19:05:23 GMT+0200 (GTB Standard Time) 
--></script>

You can create /set an instance Date object to a specific date, using the fallowing sintaxes:
new Date("Month dd, yyyy hh:mm:ss")
new Date("Month dd, yyyy")
new Date(yy,mm,dd,hh,mm,ss)
new Date(yy,mm,dd)
new Date(milliseconds)
- Month = a string with the month name
- dd = the day of the month (2 digits)
- y = the year ('yy' for 2 digits and 'yyyy' with 4 digits)
- h = the hour
- m = minutes
- s = secunds
Example:
<script type="text/javascript"><!--
var date1 = new Date("October 15, 1996 15:16:00");         // "Month dd, yyyy hh:mm:ss"
var date2 = new Date("October 15, 1976");                // "Month dd, yyyy"
var date3 = new Date(96,10,15,15,16,00);                // yy,mm,dd,hh,mm,ss
var date4 = new Date(76,10,15);                       // yy,mm,dd
var date5 = new Date(3456789000);                   // milliseconds (since 1/1/1970)

document.write(date1 + '<br />');        // Tue Oct 15 1996 15:16:00 GMT+0300 (GTB Daylight Time)
document.write(date2 + '<br />');        // Fri Oct 15 1976 00:00:00 GMT+0300 (GTB Daylight Time)
document.write(date3 + '<br />');        // Fri Nov 15 1996 15:16:00 GMT+0200 (GTB Standard Time)
document.write(date4 + '<br />');        // Mon Nov 15 1976 00:00:00 GMT+0200 (GTB Standard Time)
document.write(date5 + '<br />');        // Tue Feb 10 1970 02:13:09 GMT+0200 (GTB Standard Time)
--></script>

- Once you have an instance of the object, you can use the properties and methods of the Date object, to get or set any portion of the date described below.

Date Object Properties

In JS, the Date object has only two properties.

Date Object Methods

- To use these methods, you need an instance of the Date object. Than you can call any of the methods by using the following syntax:
instance_name.method()

- Some examples:
1) Gets and displays the date of the current day.
<script type="text/javascript"><!--
var rightnow = new Date();
var year = rightnow.getFullYear();
var month = rightnow.getMonth() + 1;      // "+ 1" becouse the 1st month is 0
var day = rightnow.getDate();

document.write(month+ '.'+ day+ '.'+ year);        // 1.31.2011 
--></script>

2) Gets and displays the day of the week, the hour and minutes of a certain time date.
<script type="text/javascript"><!--
// an Array with the week days
var ar_days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

var a_date = new Date("October 15, 1976 18:28:00");
var day = a_date.getDay();
var hour = a_date.getHours();
var minutes = a_date.getMinutes();

document.write('Day = '+ ar_days[day]+ ' -- Hour = '+ hour+ ' -- Minutes = '+ minutes);        // Day = Friday -- Hour = 18 -- Minutes = 28 
--></script>

3) Adds two hours and 30 minutes at the current time and displays the time (hour:minutes:seconds)
<script type="text/javascript"><!--
var a_time = new Date();

// gets current hour and minutes
var now_h = a_time.getHours();
var now_m = a_time.getMinutes();

// adds 2 hours and 30 minutes
a_time.setHours(now_h+2);
a_time.setMinutes(now_m+30);

// gets datas
var hour = a_time.getHours();
var minutes = a_time.getMinutes();
var seconds = a_time.getSeconds();

document.write(hour+ ':'+ minutes+ ':'+ seconds);        // 11:28:9
--></script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
JS Date Object

Last accessed pages

  1. Get the value of the selected /checked checkboxes in a form (23932)
  2. Zuma Deluxe (27238)
  3. JavaScript strip_tags and stripslashes (2385)
  4. Qwop (20723)
  5. The Rise Of Atlantis (5142)

Popular pages this month

  1. Qwop (4729)
  2. Bubbles3 (4343)
  3. Butterfly Kyodai (3092)
  4. Zuma Deluxe (2869)
  5. Backgammon (1464)