Date and Math

Date and Math

·

4 min read

JavaScript provides a built-in Date object that allows you to work with dates and times in your code. In this blog post, we will take a deep dive into the Date object and explore the various methods and properties available for working with dates and times in JavaScript. Additionally, we will also look at the Math object that provides mathematical functions for performing basic mathematical operations.

Dates and Times

The Date object is used to work with dates and times in JavaScript. You can create a new Date object by calling the Date() constructor without any arguments to get the current date and time or by passing a string, number or a combination of both as arguments.

For example, the following code creates a new Date object with the current date and time:

let now = new Date();
console.log(now); // Current date and time

You can also create a Date object for a specific date and time by passing a string or a combination of numbers as arguments to the Date() constructor. For example, the following code creates a Date object for January 1, 2023:

let newYear = new Date("2023-01-01");
console.log(newYear); // January 1, 2023

The Date object has several methods that allow you to get and set different parts of the date and time, such as the year, month, and day.

For example, the following code gets the current year:

let now = new Date();
let year = now.getFullYear();
console.log(year); // Current year

You can also use the setFullYear() method to set the year of a Date object. For example, the following code sets the year of the newYear object to 2024:

newYear.setFullYear(2024);
console.log(newYear); // January 1, 2024

The Date object also provides methods for formatting and parsing dates and times. The toString() method returns a string representation of a Date object in the format "Day Month Date Time TimeZone Year". The toUTCString() method returns a string representation of a Date object in the UTC format "Day, Date Month Year Time TimeZone".

Math

The Math object provides properties and methods for mathematical constants and functions. It's a built-in object that has properties and methods for mathematical constants, such as Math.PI and Math.E, and methods for performing mathematical operations, such as Math.round(), Math.random(), Math.max(), Math.min(), Math.abs(), etc.

For example, the following code uses the Math.round() method to round a number to the nearest integer:

let num = 5.6;
let rounded = Math.round(num);
console.log(rounded); // 6

The Math.random() method returns a random number between 0 and 1. For example, the following code generates a random number between 1 and 10:

let randomNum = Math.floor(Math.random() * 10) + 1;
console.log(randomNum); // random number between 1 and 10

console.log(Math.round(4.5), "round")     //round return the nearest number 
console.log(Math.floor(4.9) ,"floor")    //it return the small number only
console.log(Math.ceil(4.3) ,"ceil")      //it return the bigger num. only
console.log(Math.trunc(4.934) ,"trunc")  //it just remove the digits after integer
console.log(Math.sign(123) ,"sign")       //return 1 for +ve no. , -1 for -ve no. , 0 for 0
console.log(Math.sign(-3423) ,"sign") 
console.log(Math.pow(2,3) ,"pow")       //2 raise to power 3 => (2*2*2)
console.log(Math.sqrt(81) ,"sqrt")    //return square root
console.log(Math.abs(-23) ,"abs")     // return only positive no. only 
console.log(Math.random()*10,"random number") //will generate random no. from 0 to 5
console.log(Math.trunc(Math.random()*5,"random number")) 
console.log("\n")

The Math.max() and Math.min() methods are used to find the maximum and minimum values from a set of numbers respectively. For example, the following code finds the maximum and minimum values from a set of numbers:

let numbers = [2, 5, 1, 8, 9, 3];
let max = Math.max(...numbers);
console.log(max); // 9
let min = Math.min(...numbers);
console.log(min); // 1

The Math.abs() method is used to get the absolute value of a number. For example, the following code returns the absolute value of a number:

let number = -5;
let abs = Math.abs(number);
console.log(abs); // 5

The Math.pow() method is used to raise a number to a power. For example, the following code returns the result of 2 raised to the power of 3:

let result = Math.pow(2, 3);
console.log(result); // 8

The Math object also provides methods for trigonometry such as Math.sin(), Math.cos(), Math.tan(), etc. These methods return the sine, cosine, and tangent of a given angle respectively.

In conclusion, the Date object and Math object are two powerful built-in objects in JavaScript that allow you to perform a wide range of date and time-related operations as well as mathematical operations. Understanding the various properties and methods provided by these objects can help you write more efficient and effective code.