JavaScript Tutorial
Estimated reading: 3 minutes 293 views

JavaScript Date & Math โ€“ Complete Guide to Date/Time & Math Functions (2025)


Introduction โ€“ Why Learn JavaScript Date & Math?

Working with dates and math operations is essential in almost every JavaScript projectโ€”from validating form inputs and calculating deadlines to generating random numbers or computing statistics. JavaScript provides the Date object and Math global object to make this easier.

In this guide, youโ€™ll learn:

  • How to work with dates and times using Date methods
  • Formatting, comparing, and validating date values
  • Using Math functions like round(), sqrt(), random(), etc.

Topics Covered

Topic Description
Date & Time (Formats, Get/Set, Compare)Handle, validate, and manipulate dates
Math FunctionsPerform mathematical calculations
Math.randomGenerate random values for games, utilities, etc.

JavaScript โ€” Date & Time

Create a Date Object

const now = new Date();
console.log(now); // Current date & time

Create from string or parts:

const specific = new Date("2025-06-18");
const custom = new Date(2025, 5, 18, 12, 0); // Note: Month is 0-indexed

Get Methods

now.getFullYear(); // 2025
now.getMonth();    // 5 (June)
now.getDate();     // 18
now.getDay();      // 3 (Wednesday)
now.getHours();    // 13
now.getMinutes();  // 0
now.getSeconds();  // 0

Set Methods

now.setFullYear(2030);
now.setMonth(11); // December
now.setDate(25);

Compare Dates

const d1 = new Date("2025-01-01");
const d2 = new Date("2025-12-31");
console.log(d1 < d2); // true

Validate Date

const date = new Date("Invalid date");
console.log(isNaN(date.getTime())); // true โ†’ Invalid

Format Date to String

now.toDateString();   // Wed Jun 18 2025
now.toISOString();    // 2025-06-18T07:00:00.000Z
now.toLocaleDateString(); // Based on locale

JavaScript โ€” Math Functions

The Math object provides standard arithmetic, algebraic, and trigonometric functions.

Basic Functions

Math.round(4.7);    // 5
Math.floor(4.9);    // 4
Math.ceil(4.1);     // 5
Math.abs(-5);       // 5
Math.sqrt(49);      // 7
Math.pow(2, 3);     // 8

Max, Min, and Trigonometry

Math.max(10, 20, 5);   // 20
Math.min(10, 20, 5);   // 5
Math.sin(Math.PI / 2); // 1
Math.cos(0);           // 1

๐ŸŽฒ JavaScript โ€” Math.random()

Generates a pseudo-random number between 0 and 1:

Math.random(); // 0.1234567...

Random Between Range

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomInt(1, 100)); // e.g., 57

Summary โ€“ Recap & Next Steps

JavaScriptโ€™s Date and Math objects are powerful tools for managing time and numerical operations. From formatting dates to generating random numbers, they offer flexibility for both simple and complex tasks.

Key Takeaways:

  • Date helps you create, compare, set, and format dates
  • Math provides built-in functions for numeric calculations
  • Math.random() is great for games, simulations, and randomized outputs

Real-World Relevance:
Used in form validation, countdowns, random selections, analytics dashboards, and time-based logic in apps.


FAQs

Q1: How do I compare two dates in JavaScript?

Use <, >, or getTime() to compare date objects numerically.

Q2: Whatโ€™s the difference between Math.floor() and Math.round()?

Math.floor() always rounds down, Math.round() rounds to the nearest integer.

Q3: Can I get the current timestamp?

Yes, use Date.now() or new Date().getTime().

Q4: How do I get a random number between 10 and 50?

Use:

Math.floor(Math.random() * 41) + 10;

Q5: Is Math.random() secure for passwords?

No. Use crypto-based libraries for secure randomness, not Math.random().


Share Now :
Share

๐Ÿ“† JavaScript Date & Math

Or Copy Link

CONTENTS
Scroll to Top