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 tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
JS Date Object

Last accessed pages

  1. Ajax-PHP Chat Script (27384)
  2. JavaScript code and PHP (13755)
  3. Create simple Website with PHP (12453)
  4. Dream Fruit Farm (273)
  5. Mahjongg Alchemy (3348)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (49383)
  2. Read Excel file data in PHP - PhpExcelReader (37738)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (36753)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (33179)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (30548)