Showing posts with label Date Object. Show all posts
Showing posts with label Date Object. Show all posts

JavaScript : Array Object [1/2]

An array is a special variable, which can hold more than one value, at a time. You can refer to a particular element in an array by referring to the name of the array and the index number.

Syntax 1
    ArrayName = new Array();
    ArrayName [0] ="value1";
    ArrayName [1] ="value2";
   ...
    ArrayName [N] ="valueN";

Syntax 2
    ArrayName  = new Array( value1",value2" ... ,valueN");

Syntax 3
     ArrayName   = [value1",value2" ... ,valueN"];

Retrieve Value 1
    ArrayName  ArrayName[0];
    ArrayName +=  ArrayName[1];
   ...
    ArrayName +=  ArrayName[N];

Retrieve Value 2
   for (variableName in  ArrayName ) {
      ArrayName[variableName];
   }

Example with Date
<script language="JavaScript">
   var DayName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
   var d = new Date();
   var output = "Today is " + DayName[d.getDay()] + " ";
   output += d.getDate()+ "/";
   output += (d.getMonth() + 1) + "/";
   output += d.getYear() + " ";
   output += d.getHours() + ":";
   output += d.getMinutes() + ":";
   output += d.getSeconds();
   document.write(output);
</script>


figure 1 : Sample with Date

##############################

JavaScript : Date Object << Previous Chapter || Next Chapter >> JavaScript Array Object [2/2]

JavaScript : Date Object

The JavaScript Date object is used to work with dates and times. This object provide any unit of time such as Date, Month, Year, Hours, Milliseconds and etc.

Syntax
   variableName = new Date();
   variableName.methodName();

Method Name
Description
getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getMonth() Returns the month (from 0-11)
getYear() Returns the year (four digits)
getHours() Returns the hour (from 0-23)
getMinutes() Returns the minutes (from 0-59)
getSeconds() Returns the seconds (from 0-59)
getMilliseconds()  Returns the milliseconds (from 0-999)

Example
<script language="JavaScript">
   var d = new Date();
   var output = "Today : " + d.getDate()+ "/";
   output += (d.getMonth() + 1) + "/";
   output += d.getYear() + " ";
   output += d.getHours() + ":";
   output += d.getMinutes() + ":";
   output += d.getSeconds();
   document.write(output);
</script>


figure 1: Example

If you want to show the day of the week such as Monday, Tuesday and etc.
You should using Array() because getDay() will return only a number.

##############################

Next Chapter >> JavaScript : Array Object
Related Posts Plugin for WordPress, Blogger...