Вы находитесь на странице: 1из 6

Javascript date Object:

The Date object is a datatype built into the JavaScript language. Date objects are created with the new
Date( ) as shown below.
Once a Date object is created, a number of methods allow you to operate on it. Most methods simply
allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object,
using either local time or UTC (universal, or GMT) time.

The ECMAScript standard requires the Date object to be able to represent any date and time, to
millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus
273,785 years, so the JavaScript is able to represent date and time till year 275755.
There are different variants for the date object
1. new Date( )
2. new Date(milliseconds)
3. new Date(datestring)
4. new Date(year,month,date[,hour,minute,second,millisecond ])
Here is the description of the parameters:
No Argument: With no arguments, the Date( ) constructor creates a Date object set to the current date
and time.
•milliseconds: When one numeric argument is passed, it is taken as the internal numeric representation
of the date in milliseconds, as returned by the getTime( ) method. For example, passing the argument
5000 creates a date that represents five seconds past midnight on 1/1/70.
•datestring:When one string argument is passed, it is a string representation of a date, in the format
accepted by the Date.parse( ) method.
•7 agruments: To use the last form of constructor given above, Here is the description of each
argument:
1.year: Integer value representing the year. For compatibility (in order to avoid the Y2K
problem), you should always specify the year in full; use 1998, rather than 98.
2.month: Integer value representing the month, beginning with 0 for January to 11 for
December.
3.date: Integer value representing the day of the month.
4.hour: Integer value representing the hour of the day (24-hour scale).
5.minute: Integer value representing the minute segment of a time reading.
6.second: Integer value representing the second segment of a time reading.
7.millisecond: Integer value representing the millisecond segment of a time reading.
<html>

<body>

<script type="text/javascript">

var d = new Date();

// GETDATE

document.write("getDate(): Returns the month day of the current date::"+d.getDate()+"<BR>");

// FOR DATE AND TIME

var dt = Date();

document.write("Date and Time : " + dt + "<BR>");

// TO SPECIFY A DATE

var d1=new Date("Sep 19, 1983 01:15:00");

document.write("getDate(): Returns the month day of the specified date:: "+d1.getDate()+"<BR>");

// TO GET DAY OF THE WEEK

document.write("getDay():Returns the month day of the current date::"+d1.getDay()+"<BR>");

//TO GET FUULYEAR - 4 DIGITS

document.write("getFullYear() : Returns the year in 4 digits:: "+d.getFullYear()+"<BR>");

// TO GET FULYEAR FROM SPECIED DATE

var d1 = new Date("December 25, 1996");

document.write("getFullYear() : Returns the year in 4 digits ::" + d1.getFullYear()+"<BR>" );

// TIME
document.write("getTime():Returns milliseconds since 1970/01/01---"+ d.getTime() +
":MILLISECOND<br>");

// TO GET TIMEING HOURS

document.write("getHours() : RETURNS HOURS ::"+d.getHours()+"<BR>");

var d1=new Date("Sep 19, 1983 22:15:00");

document.write("getHours() : RETURNS HOURS ::"+d1.getHours()+"<BR>");

// similarly use - getMilliseconds(), getMinutes(),getMonth(),getSeconds()

// functions used to set data about date :

var d2 = new Date();

d2.setDate(23);

document.write(d2);

//SET FULL YEAR

d.setFullYear(2020);

document.write(d);

//SET : setHours(), setMinutes, setSeconds, setMilliseconds

//settime

var d = new Date();

d.setTime(-71564221);

document.write(d);

</script>

</body>
</html>
Array object in javascript

1) Create an Array
An array can be defined in three ways.
The following code creates an Array object called myCars:

1: regular array (add an optional integer argument to control array's size)


var exar=new Array();
exar[0]="S";
exar[1]="V";
exar[2]="B”;

2: condensed array
Var exar=new Array("S","V","B”);

3: literal array

var excar=["S","V","B”];

2) Accessing array elements

document.write(exar[0]);

<html>
<body>

<script type="text/javascript">

var vow = ["a","e"];


document.write("Original length: " + vow.length+"<br>");
document.write("<br />");
//vow.length=10;
document.write("New length: " + vow.length+"<br>");
var vow1 = ["o","u","i"];
//concat
var vowel = (vow.concat(vow1));
document.write(vowel+"<br>");

//join
document.write(vow1.join() + "<br>");
document.write(vow1.join("+") + "<br>");
document.write(vow1.join(" and ")+"<br>");

//pop
document.write(vow1.pop() + "<br>");
document.write(vow1 + "<br>");
document.write(vow1.pop() + "<br>");
document.write(vow1+"<br>");

//push
document.write("length"+vow.length+"<br>");
vow.push("K") ;
document.write("length"+vow.length+"<br>");
vow.push("L","P");
document.write(vow+"<br>");

// reverse of an array
document.write(vow.reverse()+"<br>");

//shift
document.write("shifted element is "+vow.shift() + "<br>");
document.write(vow+"<br>");

//slice : array.slice(start, end)


document.write("the sliced elements are :"+vow.slice(0,1) + "<br>");
document.write("the array is "+vow+"<br>");

document.write("sliced 1"+vow.slice(1) + "<br />");


document.write("sliced -2" +vow.slice(-2) + "<br />");
document.write(vow+"<br>");
//sort
document.write("the sorted array list is " +vow.sort()+"<br>");
//sort functions and sort
function sortNum(a,b)
{
return a - b;
}

var num = ["15", "2", "78", "2", "100", "1"];


document.write(num.sort(sortNum));

</script>

</body>
</html>

Вам также может понравиться