Monday, April 6, 2015

How to print date in Java Script

2:01 AMMonday, April 6, 2015
Date object enables Java Script programmers to create an object that contains information about a particular date and provides a set of methods to work with that information. To create an instance of the date object, use the keyword new as follows:
var my_date = new Date(parameters);
The parameter, if left empty, indicates today's date & time . The parameter could be any specific date format.

Date object provides the following methods.

getDate( ) : Returns date of the month as an integer from 1 to 31.
setDate( ) : Sets date of the month based on an integer argument from 1 to 31.
getHours( ) : Returns hours as an integer from 0 to 23.
setHours( ) : Sets hours based on an argument from 0 to 23.
getTime( ) : Returns number of milliseconds since 1 feb 2014 at 00:00:00
setTime( ) : Sets time based on an argument representing the number of Milliseconds since 1 feb 2014 at 00:00:00.

Date class gives the current date. You can take Date class object in a single variable like

Var today= new Date();

This object represent the current date. You know very well that every line of code in java script is known as statement. Lets take an simple example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<script type ="text/javascript" >
    var todaydate = new Date();
    document.write("Date is" + todaydate.toString());


Code generate the following output
How to print date in Java Script

Protected by Copyscape Online Copyright Protection Software


Post a Comment

Toggle Footer