Exploring the Math and Date Objects in JavaScript: A Comprehensive Guide
Math is a built-in object in JavaScript that provides the programmer with various useful methods and attributes. It helps to perform operation on user input and return the output after some mathematical operation.
Methods of Math Object
We discuss few important properties and methods of this object below:
1. Math.PI
It is property of the object that represents the ratio value of circumference of the circle to its diameter which is nearly equal to 3.14159.
2. Math.E
It is property of the object that represents Euler’s number which is nearly equal to 2.71828.
3. Math.abs(x)
It is method that returns the absolute value of a number.
4. Math.ceil(x)
It is method that returns the ceiling value of the given input number. It rounds up the given value to the nearest integer.
5. Math.floor(x)
It is method that returns the floor value of the given input number. It rounds down the given value to the nearest integer.
6. Math.max(x1,x2,…, xn)
It is a method that returns maximum value among provided numbers.
7. Math.min(x1,x2,…, xn)
It is a method that returns minimum value among provided numbers.
8. Math.random()
It generates a random floating number between 0 (inclusive) and 1 (exclusive).
9. Math.sqrt(x)
It returns square root of the given number.
10. Math.pow(x, y)
It returns the value of x raised to the power of y.
11. Math.sin(x)
It returns the sine of angle of x in radians. Similarly, Math.cos(x) and Math.tan(x) returns cosine and tangent of angle in the radians.
console.log(Math.PI); // Output: 3.141592653589793
console.log(Math.E); // Output: 2.718281828459045
console.log(Math.abs(-5)); // Output: 5
console.log(Math.abs(2.5)); // Output: 2.5
console.log(Math.ceil(3.2)); // Output: 4
console.log(Math.ceil(7.8)); // Output: 8
console.log(Math.floor(3.8)); // Output: 3
console.log(Math.floor(9.2)); // Output: 9
console.log(Math.max(2, 5, 1, 8)); // Output: 8
console.log(Math.max(-1, -5, -3)); // Output: -1
console.log(Math.min(4, 7, 2, 9)); // Output: 2
console.log(Math.min(-1, -5, -3)); // Output: -5
console.log(Math.random()); // Output: Random decimal value between 0 and 1
console.log(Math.sqrt(16)); // Output: 4
console.log(Math.sqrt(25)); // Output: 5
console.log(Math.pow(2, 3)); // Output: 8 (2 raised to the power of 3)
console.log(Math.pow(5, 2)); // Output: 25 (5 raised to the power of 2)
console.log(Math.sin(Math.PI / 2)); // Output: 1 (sine of 90 degrees in radians)
console.log(Math.cos(0)); // Output: 1 (cosine of 0 degrees in radians)
console.log(Math.tan(Math.PI / 4)); // Output: 1 (tangent of 45 degrees in radians)
Date Object
Date object is a built-in object in JavaScript that helps us to manipulate and access various time parameters. It provides us with functions that helps to retrieve specific time values.
Methods of Date Object
The methods of the object are discussed below with code examples:
1. Creating Date Object
let currentDate = new Date(); // Creates a Date object with the current date and time
console.log(currentDate);
Unlike Math object, we have to use constructor to create a Date object to access its methods and properties.
2. Getting Current Date And Time
let currentDate = new Date();
console.log(currentDate.toString()); // Output: Mon Oct 11 2023 14:30:00 GMT+0000 (Coordinated Universal Time)
The method toString() returns values of current data and time in form of string.
3. Getting Specific Components of Time
let currentDate = new Date();
console.log(currentDate.getFullYear()); // Output: 2023 (current year)
console.log(currentDate.getMonth()); // Output: 9 (months are zero-based, so October is represented by 9)
console.log(currentDate.getDate()); // Output: 11 (current day of the month)
console.log(currentDate.getHours()); // Output: 14 (current hour)
console.log(currentDate.getMinutes()); // Output: 30 (current minute)
console.log(currentDate.getSeconds()); // Output: 0 (current second)
console.log(currentDate.getMilliseconds()); // Output: 0 (current millisecond)
console.log(currentDate.getDay()); // Output: 1 (current day of the week, where Sunday is 0 and Monday is 1)
After creating a Date object, we can call its methods to get only specific component of time like year, month, day, etc. For example: The getFullYear() method returns the current year. It is important to remember that months and day of the week starts from 0 i.e. January and Sunday is returned as “0” by these functions.
4. Setting Specific Components of Time
let currentDate = new Date();
currentDate.setFullYear(2024);
currentDate.setMonth(11); // Months are zero-based, so December is represented by 11
currentDate.setDate(25);
console.log(currentDate.toString()); // Output: Thu Dec 25 2024 14:30:00 GMT+0000 (Coordinated Universal Time)
Similar to getting methods above, we can use setter functions to create our own Date object by providing required values.
5. Converting Date to Specific String Format
let currentDate = new Date();
console.log(currentDate.toDateString()); // Output: Mon Oct 11 2023
console.log(currentDate.toLocaleDateString()); // Output: 10/11/2023 (formatted based on the user's locale)
console.log(currentDate.toISOString()); // Output: 2023-10-11T14:30:00.000Z (ISO 8601 format)
We can call these above functions to get Date string in various formats.
6. Finding Difference of Time
let currentDate = new Date();
let futureDate = new Date("December 31, 2023");
let timeDifference = futureDate.getTime() - currentDate.getTime();
let daysDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24)); // Calculating the difference in days
console.log(daysDifference); // Output: 81 (number of days remaining until December 31, 2023)
We created two objects for current and future time respectively and subtract them after calling getTime() function on both of them. Later, we converted it to days and print it out.
This way, we can use Math and Date Object to manipulate numbers and time respectively. This concludes the basic understanding of JavaScript built-in objects.