JavaScript Tutorial
Estimated reading: 3 minutes 11 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“† JavaScript Date & Math

Or Copy Link

CONTENTS
Scroll to Top