How to Create a Date And Math Object in JavaScript

Introduction 

 
In this article, we will learn how to create date and math objects in JavaScript. In my previous article, I explained about Objects in the JavaScript link.
 

Object

 
An object is an unordered collection of data. It is an entity having state and behavior properties and methods. For example, pen, chair, car, etc.…
 

Date Object

 
The JavaScript date object can be used to get year, month and day. You can display a timer on the webpage using the JavaScript date object. It stores date and time.
 

Uses of Date Object

  • Creation/Modification of time
  • To display the current date and time
  • To measure time
  • You can use different Date constructors to create a date object

Creation of Date Object

 
To create a date object using the ‘new’ keyword.
  • new Date ()
  • new Date (milliseconds)
  • new Date (date string)
  • new date (date, month, year, hour, minutes, seconds, milliseconds)
Let’s see one example of displaying the date using the keyword new date ().
 
Example
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Date Object in JavaScript</title>  
  5. </head>  
  6. <body>  
  7.     <h2>Date Object in JavaScript</h2>  
  8.     <h3>To Display the Current Date</h3>  
  9.     <script type="text/javascript">  
  10.             var date=new Date();  //to display current date  
  11.             var day=date.getDate();    
  12.             var month=date.getMonth()+1; //It Starts with zero   
  13.             var year=date.getFullYear();   
  14.             document.write("<br>Current Date is: "+day+"/"+month+"/"+year);    
  15.     </script>  
  16. </body>  
  17. </html>  
Output
 
How To Create Date And Math Object In JavaScript
 
Display the current time
 
Example
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Date Object in JavaScript</title>  
  5.     <meta charset="utf-8">  
  6. </head>  
  7. <body>  
  8.     <h2>Date Object in JavaScript</h2>  
  9.     <h3>To Display the Current Time</h3>  
  10.     <script type="text/javascript">  
  11.         var today =new Date();    
  12.         var hour=today.getHours();  //current time   
  13.         var minute=today.getMinutes();    
  14.         var second=today.getSeconds();    
  15.         document.write("Current Time is :"+hour+":"+minute+":"+second);  
  16. </script>  
  17. </body>  
  18. </html>  
Output
 
How To Create Date And Math Object In JavaScript
 

Date Method in JavaScript

 
When a date object is created, there are several methods that make it possible to perform its functions. Let's see these methods with their descriptions. 
 
Methods
Description
get Date ()
Gets the day of the month
get Day ()
Gets the day of the week
get Month ()
Gets the month
get Fullyear ()
Gets the year
get Hour ()
Gets the hour
get Minutes ()
Gets the Minutes
get seconds ()
Gets the Seconds
get milliseconds ()
Gets the milliseconds
 

Setinterval () method

 
This will call the alert function every 3 seconds (1000 ms = 1 second).
 
Example
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Date Object in JavaScript</title>  
  5.     <meta charset="utf-8">  
  6. </head>  
  7. <body>  
  8.     <h2>JavaScript set method</h2>  
  9.     <script type="text/javascript">  
  10.         function alerts()   
  11.         {  
  12.             alert("Hello! Welcome.");  
  13.         }  
  14.         setInterval(alerts,5000)   
  15. </script>  
  16. </body>  
  17. </html>  
Output
 
How To Create Date And Math Object In JavaScript
 

The Math Object in JavaScript

 
The Math object allows for mathematical operation and includes a number of methods and properties that are used for calculations.
 
Properties
 
Properties
Description
E
Euler’s constant
LN2
Natural Log of the value
LN10
Natural log of the value 10
LOG2E
The base 2 log of Euler's Constant (E)
LOG10E
The base 10 log of Euler's Constant (E)
PI
Returns the Constant PI
 
The following Example will calculate the Pi constant value
 
Example
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Math Object in JavaScript</title>  
  5.     <meta charset="utf-8">  
  6. </head>  
  7. <body>  
  8.     <h2>Math Object Pi Constant</h2>  
  9.     <script type="text/javascript">  
  10.         document.write("Pi Constant :");  
  11.         document.write(Math.PI);  
  12. </script>  
  13. </body>  
  14. </html>  
Output
 
How To Create Date And Math Object In JavaScript
 

The Math Object Method

 
The Math object contains a number of methods that are used for calculations. There are some methods below:
 
Methods
Description
sqrt(x)
Returns the square root of x
log(x)
Returns the natural logarithm (base E) of x
exp(x)
Returns the value of Ex
pow(x,y)
Returns the value of x to the power of y
abs(x)
Returns the absolute value of x
random()
Returns a random number between 0 and 1
 
The following example will calculate the Square root of a number:
 
Example
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Math Object in JavaScript</title>  
  5.     <meta charset="utf-8">  
  6. </head>  
  7. <body>  
  8.     <h2>Math Object Methods</h2>  
  9.     <script type="text/javascript">  
  10.         var number = Math.sqrt(prompt("Enter the Number"));   
  11.         document.write("The Square Root is :"+number);  
  12. </script>  
  13. </body>  
  14. </html>  
Output
 
How To Create Date And Math Object In JavaScript
 
How To Create Date And Math Object In JavaScript
 
The following Example will calculate the power (x,y) of a number:
 
Example
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4.     <title>Math Object in JavaScript</title>    
  5.     <meta charset="utf-8">    
  6. </head>    
  7. <body>    
  8.     <h2>Math Object Methods</h2>    
  9.     <script type="text/javascript">    
  10.         document.write("Math.pow(x,y) returns the value of x to the power of y:")    
  11.         document.write(Math.pow(5,2));    
  12.     
  13. </script>    
  14. </body>    
  15. </html>     
Output
 
How To Create Date And Math Object In JavaScript
 

Summary

 
We have seen about the Date and Math Objects in this article. I hope this article was useful to you!