Home | | Internet Programming | JavaScript - Date, Array and Math Object

Chapter: Web or internet Programming : Scripting Elements

JavaScript - Date, Array and Math Object

The Date object is used to work with dates and times. Date objects are created with the Date() constructor.

Date Object

 

The Date object is used to work with dates and times. Date objects are created with the Date() constructor. There are four ways of instantiating a date:

new Date() // current date and time

 

new Date(milliseconds) //milliseconds since 1970/01/01 new Date(dateString)

new Date(year, month, day, hours, minutes, seconds, milliseconds)

 

Example:

 

today = new Date()

d1 = new Date("October 13, 1975 11:13:00")

 

d2 = new Date(79,5,24)

d3 = new Date(79,5,24,11,33,0)

 

Compare Two Dates

 

The Date object is also used to compare two dates. The following example compares today's date with the 15th August 2012:

 

var myDate=new Date(); myDate.setFullYear(2012,7,15); var today = new Date();

if (myDate>today)

{

 

alert("Today is before 15th August 2012");

}

 

else

{

 

alert("Today is after 15th August 2012");

}

 

Array Object

 

An array is a special variable, which can hold more than one value, at a time. An array can be defined in three ways. The following code creates an Array object called myCars:

 

1.    var myCars=new Array(); // regular array (add an optional integer myCars[0]="Saab"; // argument to control array's size) myCars[1]="Volvo";

myCars[2]="BMW";

 

2.     var myCars=new Array("Saab","Volvo","BMW"); // condensed array

3.     var myCars=["Saab","Volvo","BMW"]; // literal array

 

Math Object

 

The Math object allows you to perform mathematical tasks. The Math object includes several mathematical constants and methods.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Scripting Elements : JavaScript - Date, Array and Math Object |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.