Java Script


JavaScript Tutorial


Admission Enquiry Form


Date object in JavaScript

In JavaScript, object is the container of properties/variables and methods/functions. Date is the object which contains some methods to perform some date and time related functions.

Date properties and methods in JavaScript.

Example of creating date object in JavaScript

Creating Date() object and accessing getHours(), getMinutes() and getSeconds() methods

<script>
var d=new Date();
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
document.write(h+":"+m+":"+s);
</script>



Output:

The code above will display :

16:36:22



Example of displaying Date() object

By default, JavaScript will use the browser's time zone and display a date as a full text string:

var d=new Date();
document.write(d);


Output:

The code above will display :

Sat May 08 2021 16:43:29 GMT+0530 (India Standard Time)




Example of getFullYear() method

This method returns current year

var d=new Date();
var y=d.getFullYear();
document.write("current year = "+y);


Output:

The code above will display :

current year = 2021




Example of getMonth() method

This method returns the current month

var d=new Date();
var m=d.getMonth();
document.write("current month = "+m);



Output:

The code above will display :

current month = 4




Example of getDate() method

This method returns the current date

var d=new Date();
var date=d.getDate();
document.write("current date = "+date);



Output:

The code above will display :

current date = 8




Example of getTime() method

This methods returns the number of milliseconds* since the Unix Epoch. *. This time is the number of seconds that have passed since 1 January 1970 00:00 UTC(Coordinated Universal Time)

var d=new Date();
var t=d.getTime();
document.write("current unix seconds = "+t);



Output:

The code above will display :

current unix milliseconds = 1620482229477