Date and Time

Date and Time

Date and Time in JavaScript

JavaScript provides the Date object to work with dates and times. You can create, modify, and format dates using built-in methods.

Creating a Date Object

There are multiple ways to create a date in JavaScript:

Current Date and Time

let now = new Date(); console.log(now); // Output: Current date and time (e.g., 2025-03-11T12:34:56.789Z)

Specific Date and Time (YYYY, MM, DD, HH, MM, SS, MS)

let specificDate = new Date(2025, 2, 11, 14, 30, 0); // Month starts from 0 (0 = January) console.log(specificDate); // Output: Tue Mar 11 2025 14:30:00

Date from a String

let fromString = new Date("2025-03-11T14:30:00"); console.log(fromString); // Output: Tue Mar 11 2025 14:30:00

Date from a Timestamp (Milliseconds since Jan 1, 1970)

let timestampDate = new Date(1710151800000); console.log(timestampDate);

Getting Date Components

You can extract parts of a date using the get methods.

let date = new Date(); console.log(date.getFullYear()); // 🟢 Year (e.g., 2025) console.log(date.getMonth()); // 🟢 Month (0-11, where 0 = January) console.log(date.getDate()); // 🟢 Day of the month (1-31) console.log(date.getDay()); // 🟢 Day of the week (0-6, where 0 = Sunday) console.log(date.getHours()); // 🟢 Hours (0-23) console.log(date.getMinutes()); // 🟢 Minutes (0-59) console.log(date.getSeconds()); // 🟢 Seconds (0-59) console.log(date.getMilliseconds()); // 🟢 Milliseconds (0-999) console.log(date.getTime()); // 🟢 Timestamp (milliseconds since 1970)

Setting Date Components

You can modify a date using set methods.

let date = new Date(); date.setFullYear(2026); // Change year to 2026 date.setMonth(5); // Change month to June (5 = June) date.setDate(20); // Change day to 20 date.setHours(10); // Change hour to 10 AM console.log(date);

Formatting Dates

JavaScript provides methods to convert dates into strings.

Convert to Date String

console.log(date.toDateString()); // Output: Tue Jun 20 2026 console.log(date.toISOString()); // Output: 2026-06-20T10:00:00.000Z console.log(date.toUTCString()); // Output: Sat, 20 Jun 2026 10:00:00 GMT console.log(date.toLocaleString()); // Output: 6/20/2026, 10:00:00 AM (format depends on locale) console.log(date.toLocaleDateString()); // Output: 6/20/2026 console.log(date.toLocaleTimeString()); // Output: 10:00:00 AM

Comparing Dates

let date1 = new Date(2025, 2, 11); let date2 = new Date(2026, 5, 20); console.log(date1 > date2); // false console.log(date1 < date2); // true console.log(date1.getTime() === date2.getTime()); // false

Working with Timestamps

let now = Date.now(); // Current timestamp in milliseconds console.log(now); let fiveDaysLater = now + 5 * 24 * 60 * 60 * 1000; // Add 5 days console.log(new Date(fiveDaysLater));

Date Manipulation with Intl.DateTimeFormat

let formatter = new Intl.DateTimeFormat("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric" }); console.log(formatter.format(new Date())); // Output: "Tuesday, March 11, 2025"

🎯 Summary

Create dates with new Date(), timestamps, or strings.
Get parts of a date using getFullYear(), getMonth(), getDate(), etc.
Modify dates with setFullYear(), setMonth(), setDate(), etc.
Format dates with toLocaleString(), toDateString(), etc.
Compare dates using getTime().

🚀 JavaScript provides powerful tools to work with dates and times! Let me know if you need more examples. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close