How to use date in javascript and the different date methods?

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ). Date expression with arguments

  • new Date( )
  • new Date(datestring)
  • new Date(year,month,date[,hour,minute,second,millisecond ])

Description of the parameters:

  • No Argument:With no arguments, the Date( ) constructor creates a Date object set to the current date and time.
  • 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:
    • 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.
    • month: Integer value representing the month, beginning with 0 for January to 11 for December.
    • date: Integer value representing the day of the month.
    • hour: Integer value representing the hour of the day (24-hour scale).
    • minute: Integer value representing the minute segment of a time reading.
    • second: Integer value representing the second segment of a time reading.
    • millisecond: Integer value representing the millisecond segment of a time

Date Methods:

  •  Date()            – Returns today’s date and time
  • getDate()        -Returns the day of the month for the specified date according to local time.
  • getDay()         -Returns the day of the week for the specified date according to local time.
  • getFullYear() -Returns the year of the specified date according to local time.
  • getHours()      -Returns the hour in the specified date according to local time.
  • getMonth()     -Returns the month in the specified date according to local time.
  • getSeconds()  -Returns the seconds in the specified date according to local time.
  • getTime()        -Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • getTimezoneOffset()   -Returns the time-zone offset in minutes for the current locale.
  • getUTCDate()        -Returns the day (date) of the month in the specified date according to universal time.
  • getUTCDay()          -Returns the day of the week in the specified date according to universal time.
  • getUTCFullYear()   -Returns the year in the specified date according to universal time.
  • getUTCHours()      -Returns the hours in the specified date according to universal time.
  • getUTCMilliseconds()     -Returns the milliseconds in the specified date according to universal time.
  • getUTCMinutes()            -Returns the minutes in the specified date according to universal time.
  • getUTCMonth()  -Returns the month in the specified date according to universal time.
  • getUTCSeconds()    -Returns the seconds in the specified date according to universal time.

Array in Javascript

Array is a variable that is used to hold more than one value in a single variable. That is we can hold a list of data inside an array.

How to declare an array in javascript ?

We can declare an array in two ways. these are the ways we can achieve it.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayName2 = ["data 1", "data 1", "data 3"];

How to add elements to array in javascript ?

Javascript array has a method push, the method we can use to add new elements to the array. when we use push the element will be added at the end of the array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.push("data 4");
var arrayNewLength=arrayName1.length; // output will be 4

How to delete an elements from array in javascript?

Javascript array has method named pop , it help us to delete an element from an array. when we use pop the elements at the end will be deleted.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.pop();
var arrayNewLength=arrayName1.length; // output will be 2

How to find number of elements in an array in javascript ?

Javascript array has a property called length , the length property help us to find the number of elements exist in an array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3

What is comments and types of comments in javascript?

                          For two reasons we can have comments in the code, one is to define the description of the specific code and the another thing is to prevent the execution of code .

There are two type of comments are available in javascript

1. Single line comment
2. Multiple line comment

Single line comment

Single line comment is used to comment a single line of code.

Syntax

//the code we need to comment

Example

// var total=0;

Multiple line comment

Multiple line comment is used to comment a block of coded that is multy line commenting.

Syntax

/*
the code we need to comment line 1
the code we need to comment line 2
the code we need to comment line 3
*/

Example

/*
var a=10;
var b=20;
var c=a+b;
*/

 

what is javascript function?

      Javascript function is grouping a set of codes to perform particular operations. we will invoke or execute the javascript function according to our needs.

Javascript function syntax

            The javascript function can be created by using the keyword function followed by specifying function name then the arguments inside the parentheses , then we can open the curly brackets and can write function definition inside the curly brackets.

function function_name(parameter_1,parameter_2, ……….)
{
// function_definition
}

  • function is a keyword used to define functions.
  • function_name is the name we will define for the function, any name we can use and it is user defined. the function name can include letters, numbers and underscore.
  • parameter_1, parameter_2 are the parameters list. we can specify multiple parameters by separating by the comma.
  • function_definition is the code block , the code we need to execute when the function is invoked.

Example of javascript function

// The function returns the sum of number1 and number2
function AddTwoNumbers(number1,number2)
{
return number1+number2;
}

above you can see basic javascript example for finding the sum of two numbers.

  • function is the keyword.
  • AddTwoNumbers is the function name.
  • number1 and number2 are the parameters.
  • “return number1+number2” , add two number and return the sum.

How to call javascript function

        we can call javascript function by just specifying the function name and the arguments inside the parentheses.

Javascript function calling example

var sum=AddTwoNumbers(40,60);

  • “AddTwoNumbers” is the method name
  • “40,60” are the parameter values.
  • “sum” is just a variable used to store the function returning value

Finally after executing the above function (“var sum=AddTwoNumbers(40,60);”) we will get 100 as result in the “sum” variable.

Attributes for script tag

For a basic thing, HTML element can contain some attributes. For example you can set background color in the ‘<body>’ tag by following code:

Code

<body bgcolor=’blue’>

</body>

‘<Script>’ tag can also have some attributes to set its properties. The first one is for choosing a language. Scripting has different languages such as JavaScript, VBScript, LiveScript and etc. we must define in which language we will write our codes. If you don’t write anything here, it choose JavaScript as the language for default. Look at the following example to see the usage of this attribute:

Code

<script language =”javascript”>

</script>

The code we are writing inside the script tag in the same page called internal javascript.

The another attribute for script tag is is ‘src’. It is useful for when you have a collection of JavaScript codes and you want to save it outside of current page because of some reasons (maybe you want to use them in different pages , they get a lot of space in the current page or script is in another domain). In this case, just copy your codes in a new notepad file and save it with ‘.js’ extention. then call it in ‘src’ part. For example if this file is put in the ‘js’ directory with ‘first_js.js’ as its name you can call it in this way:

Code

<script src =”js/first_js.js”>

</script>

The code we are writing in an another file and referring that file into the page is called external javascript.

What is javascript with basic example ?

Javascript is a programming language for the web apllications. Let’s start a script, Scripts are a group of codes from one language that do some specific operations . Now we want to start a simple one that show a message box when the page loads.

We must use a function to do opations, Functions are expressions that often get some input data and doing some operations on them and returning a value as a result, or do an operation and the result is visual changes in the page instead. A function for making a message box is in the second group. Look at its code then we’ll discuss more about it:

Basic Code:

<html>
<head>
<script>
alert(‘Hello’)
</script>
</head><body>
</body>
</html>

When you run this page (just when it loads), a message box will be shown:

Description: Message Box

‘alert’ is a function that shows box with text we passed as the argument. To insert input data to the functions, you must open a parenthesis. If you have a text as an input (like a message for a message box), it must be written between single our double quotes (‘text’ or “text”).