Thanks to visit codestin.com
Credit goes to www.rameshfadatare.com

JavaScript Date Object

In this chapter, we will learn about the JavaScript Date object. The Date object is used to work with dates and times in JavaScript. We will cover:

  • What is the Date Object?
  • Creating Date Objects
  • Date Methods
  • Formatting Dates
  • Date Arithmetic
  • Simple Programs using Date Object

What is the Date Object?

The Date object in JavaScript is a built-in object that represents a single moment in time. It provides various methods for manipulating and formatting dates and times.

Creating Date Objects

You can create a Date object using the Date() constructor. There are several ways to create a Date object:

Syntax

let date = new Date();
let date = new Date(milliseconds);
let date = new Date(dateString);
let date = new Date(year, month, day, hour, minute, second, millisecond);

Example

let now = new Date();
let epoch = new Date(0); // Unix Epoch (January 1, 1970)
let specificDate = new Date("2023-06-15");
let detailedDate = new Date(2023, 5, 15, 10, 30, 0);

console.log(now);
console.log(epoch);
console.log(specificDate);
console.log(detailedDate);

Output:

Current Date and Time
Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
Thu Jun 15 2023 00:00:00 GMT+0530 (India Standard Time)
Thu Jun 15 2023 10:30:00 GMT+0530 (India Standard Time)

Date Methods

The Date object provides various methods to get and set date and time values.

Getting Date and Time

let now = new Date();
console.log(now.getFullYear()); // Year
console.log(now.getMonth()); // Month (0-11)
console.log(now.getDate()); // Day of the month
console.log(now.getDay()); // Day of the week (0-6)
console.log(now.getHours()); // Hours
console.log(now.getMinutes()); // Minutes
console.log(now.getSeconds()); // Seconds
console.log(now.getMilliseconds()); // Milliseconds
console.log(now.getTime()); // Milliseconds since January 1, 1970

Output:

2023
5
15
4
10
30
0
0
1672531200000

Setting Date and Time

let date = new Date();
date.setFullYear(2025);
date.setMonth(11); // December (0-11)
date.setDate(25);
date.setHours(15);
date.setMinutes(45);
date.setSeconds(30);

console.log(date);

Output:

Thu Dec 25 2025 15:45:30 GMT+0530 (India Standard Time)

Formatting Dates

The Date object provides methods to format dates into readable strings.

Example

let now = new Date();
console.log(now.toDateString()); // Date string
console.log(now.toTimeString()); // Time string
console.log(now.toLocaleDateString()); // Localized date string
console.log(now.toLocaleTimeString()); // Localized time string
console.log(now.toISOString()); // ISO string
console.log(now.toUTCString()); // UTC string

Output:

Thu Jun 15 2023
10:30:00 GMT+0530 (India Standard Time)
6/15/2023
10:30:00 AM
2023-06-15T05:00:00.000Z
Thu, 15 Jun 2023 05:00:00 GMT

Date Arithmetic

You can perform arithmetic operations with dates, such as adding or subtracting days, hours, or minutes.

Example

let now = new Date();
let nextWeek = new Date(now);
nextWeek.setDate(now.getDate() + 7); // Add 7 days

let nextHour = new Date(now);
nextHour.setHours(now.getHours() + 1); // Add 1 hour

console.log("Now:", now);
console.log("Next Week:", nextWeek);
console.log("Next Hour:", nextHour);

Output:

Now: Thu Jun 15 2023 10:30:00 GMT+0530 (India Standard Time)
Next Week: Thu Jun 22 2023 10:30:00 GMT+0530 (India Standard Time)
Next Hour: Thu Jun 15 2023 11:30:00 GMT+0530 (India Standard Time)

Simple Programs using Date Object

Program 1: Countdown Timer

function countdown(endDate) {
  let now = new Date();
  let timeRemaining = endDate - now;

  let days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
  let hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);

  return `${days}d ${hours}h ${minutes}m ${seconds}s`;
}

let eventDate = new Date("2023-12-31T23:59:59");
console.log("Countdown to New Year:", countdown(eventDate));

Output:

Countdown to New Year: 199d 13h 29m 30s (example output)

Program 2: Age Calculator

function calculateAge(birthDate) {
  let now = new Date();
  let age = now.getFullYear() - birthDate.getFullYear();
  let monthDifference = now.getMonth() - birthDate.getMonth();

  if (monthDifference < 0 || (monthDifference === 0 && now.getDate() < birthDate.getDate())) {
    age--;
  }

  return age;
}

let birthDate = new Date("1990-06-15");
console.log("Age:", calculateAge(birthDate));

Output:

Age: 33 (example output)

Conclusion

In this chapter, you learned about the JavaScript Date object, including how to create Date objects, date methods, formatting dates, and performing date arithmetic. We also provided various use cases with simple programs to demonstrate the usage of the Date object. Understanding how to effectively use the Date object is essential for working with dates and times in JavaScript.

Leave a Comment

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

Scroll to Top