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 can be used to add CSS styles directly in the HTML tag?
type style class
<div style="width: 80%; border: 3px solid #888888;">Content</div>
Which CSS transform method distorts the HTML element in a given angle (including its content)?
translate() scale() skew()
#some_id {
  transform: skew(20deg, 25deg);
  -ms-transform: skew(20deg, 25deg);   /* IE 9 */
  -webkit-transform: skew(20deg, 25deg);   /* Safari and Chrome */
}
Click on the function which converts a Date object to a string.
indexOf() toString() getDate()
var rightnow = new Date();
alert( rightnow.toString() );
Which function applies a callback function to the elements of the given array?
array_merge() array_search() array_map()
$arr = arra("abc", "<p>xyz</p>", "<em>PHP</em>");
// apply the strip_tags() function to delete HTML tags from each array item
$arr = array_map("strip_tags", $arr);
}
JS Date Object

Last accessed pages

  1. Bubbles3 (47598)
  2. SHA512 Encrypt hash in JavaScript (7776)
  3. Mahjong2 (855)
  4. Mahjong Games (17769)
  5. Get Lower, Higher, and Closest Number (1574)

Popular pages this month

  1. Qwop (3030)
  2. Drag Racer V3 (1516)
  3. Bubbles3 (728)
  4. Flash Games - Free online Games (677)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (665)