Software Development Exam  >  Software Development Notes  >  JavaScript for Web Development  >  JavScript Date & Math Objects

JavScript Date & Math Objects | JavaScript for Web Development - Software Development PDF Download

Introduction

In JavaScript, the Date and Math objects are incredibly useful when working with dates, times, and performing mathematical calculations. These objects provide a wide range of functionalities that can help you manipulate, format, and perform various operations on dates and numbers. In this article, we will explore the Date and Math objects in JavaScript, providing simple code examples and explanations to help beginners understand their usage.

The Date Object

The Date object in JavaScript is used to work with dates and times. It allows you to create, manipulate, and format dates in various ways. Let's look at some of the most commonly used methods and properties of the Date object.

Creating a Date

To create a new Date object, you can simply use the new Date() constructor. Here's an example:

const currentDate = new Date();

console.log(currentDate);

Output

Sat May 22 2023 14:30:00 GMT+0530 (India Standard Time)

In the above code, we create a new Date object and assign it to the currentDate variable. When we log the currentDate, it displays the current date and time.

Getting Individual Date Components

The Date object provides various methods to retrieve individual components of a date, such as the year, month, day, hour, minute, and second. Here's an example:

const currentDate = new Date();


const year = currentDate.getFullYear();

const month = currentDate.getMonth();

const day = currentDate.getDate();

const hour = currentDate.getHours();

const minute = currentDate.getMinutes();

const second = currentDate.getSeconds();


console.log(year, month, day, hour, minute, second);

Output

2023 4 22 14 30 0

In the above code, we use the getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), and getSeconds() methods to extract individual components of the current date and time.

Formatting Dates

The Date object also provides methods to format dates and times according to specific requirements. One commonly used method is toLocaleString(), which returns a formatted string representing the date and time. Here's an example:

const currentDate = new Date();


const formattedDate = currentDate.toLocaleString();


console.log(formattedDate);

Output

5/22/2023, 2:30:00 PM

The toLocaleString() method formats the date and time according to the user's local settings.

The Math Object

The Math object in JavaScript provides a set of mathematical functions and constants that are often needed when performing calculations. Let's explore some of the commonly used methods and properties of the Math object.

Math Constants

The Math object provides several mathematical constants, such as Math.PI and Math.E, which represent the values of π (pi) and the base of the natural logarithm, respectively. Here's an example:

console.log(Math.PI);

console.log(Math.E);

Output

3.141592653589793

2.718281828459045

In the above code, we log the values of Math.PI and Math.E.

Math Functions

The Math object includes various mathematical functions that can be used to perform calculations. Let's look at a few examples:

Math.abs(): Returns the absolute value of a number.

console.log(Math.abs(-10));

Output

10

Math.pow(): Raises a number to a specified power.

console.log(Math.pow(2, 3));

Output

8

Math.sqrt(): Returns the square root of a number.

console.log(Math.sqrt(25));

Output

5

These are just a few examples of the many mathematical functions provided by the Math object.

Sample Problems

1. Calculate the area of a circle with a radius of 5 units.

const radius = 5;

const area = Math.PI * Math.pow(radius, 2);


console.log(area);

Output

78.53981633974483

2. Generate a random number between 1 and 10.

const randomNumber = Math.floor(Math.random() * 10) + 1;

console.log(randomNumber);

Output

7

Conclusion

The Date and Math objects in JavaScript provide powerful functionalities for working with dates, times, and performing mathematical calculations. By utilizing the methods and properties of these objects, you can manipulate, format, and perform various operations on dates and numbers in your JavaScript programs. Remember to refer to the official JavaScript documentation for more detailed information and additional functionalities offered by these objects. 

The document JavScript Date & Math Objects | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

MCQs

,

Extra Questions

,

mock tests for examination

,

JavScript Date & Math Objects | JavaScript for Web Development - Software Development

,

JavScript Date & Math Objects | JavaScript for Web Development - Software Development

,

Summary

,

pdf

,

Semester Notes

,

Important questions

,

Objective type Questions

,

Previous Year Questions with Solutions

,

Sample Paper

,

JavScript Date & Math Objects | JavaScript for Web Development - Software Development

,

study material

,

Free

,

video lectures

,

past year papers

,

ppt

,

practice quizzes

,

shortcuts and tricks

,

Viva Questions

,

Exam

;