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
- <!DOCTYPE html>
- <html>
- <head>
- <title>Date Object in JavaScript</title>
- </head>
- <body>
- <h2>Date Object in JavaScript</h2>
- <h3>To Display the Current Date</h3>
- <script type="text/javascript">
- var date=new Date();
- var day=date.getDate();
- var month=date.getMonth()+1;
- var year=date.getFullYear();
- document.write("<br>Current Date is: "+day+"/"+month+"/"+year);
- </script>
- </body>
- </html>
Output
Display the current time
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>Date Object in JavaScript</title>
- <meta charset="utf-8">
- </head>
- <body>
- <h2>Date Object in JavaScript</h2>
- <h3>To Display the Current Time</h3>
- <script type="text/javascript">
- var today =new Date();
- var hour=today.getHours();
- var minute=today.getMinutes();
- var second=today.getSeconds();
- document.write("Current Time is :"+hour+":"+minute+":"+second);
- </script>
- </body>
- </html>
Output
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
- <!DOCTYPE html>
- <html>
- <head>
- <title>Date Object in JavaScript</title>
- <meta charset="utf-8">
- </head>
- <body>
- <h2>JavaScript set method</h2>
- <script type="text/javascript">
- function alerts()
- {
- alert("Hello! Welcome.");
- }
- setInterval(alerts,5000)
- </script>
- </body>
- </html>
Output
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
- <!DOCTYPE html>
- <html>
- <head>
- <title>Math Object in JavaScript</title>
- <meta charset="utf-8">
- </head>
- <body>
- <h2>Math Object Pi Constant</h2>
- <script type="text/javascript">
- document.write("Pi Constant :");
- document.write(Math.PI);
- </script>
- </body>
- </html>
Output
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
- <!DOCTYPE html>
- <html>
- <head>
- <title>Math Object in JavaScript</title>
- <meta charset="utf-8">
- </head>
- <body>
- <h2>Math Object Methods</h2>
- <script type="text/javascript">
- var number = Math.sqrt(prompt("Enter the Number"));
- document.write("The Square Root is :"+number);
- </script>
- </body>
- </html>
Output
The following Example will calculate the power (x,y) of a number:
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>Math Object in JavaScript</title>
- <meta charset="utf-8">
- </head>
- <body>
- <h2>Math Object Methods</h2>
- <script type="text/javascript">
- document.write("Math.pow(x,y) returns the value of x to the power of y:")
- document.write(Math.pow(5,2));
-
- </script>
- </body>
- </html>
Output
Summary
We have seen about the Date and Math Objects in this article. I hope this article was useful to you!