TypeScript Date getDay()

In this chapter, we will explore the Date.getDay() method in TypeScript. This method returns the day of the week for the specified date according to local time. Understanding how to use Date.getDay() is useful for extracting the day of the week from a date object.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The Date.getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday and 6 represents Saturday.

2. Syntax

dateObj.getDay();

Parameters

The Date.getDay() method does not take any parameters.

Return Value

The method returns an integer number between 0 and 6, representing the day of the week for the given date according to local time.

3. Examples

Let's look at some examples to understand how Date.getDay() works in TypeScript.

Example 1: Basic Usage

In this example, we use Date.getDay() to get the day of the week from a specific date.

let date = new Date('2024-07-15');
let dayOfWeek = date.getDay();
console.log(dayOfWeek); // Output: 1 (Monday)

Example 2: Getting the Current Day of the Week

In this example, we use Date.getDay() to get the current day of the week.

let today = new Date();
let currentDayOfWeek = today.getDay();
console.log(currentDayOfWeek); // Output: The current day of the week, e.g., 2 (Tuesday)

Example 3: Using Date.getDay() with Different Dates

In this example, we use Date.getDay() to get the day of the week for different dates.

let date1 = new Date('2024-02-01');
let date2 = new Date('2024-12-31');
console.log(date1.getDay()); // Output: 4 (Thursday)
console.log(date2.getDay()); // Output: 2 (Tuesday)

Example 4: Using Date.getDay() with a Date Object

In this example, we use Date.getDay() with a date object created from the current date and time.

let date = new Date();
let dayOfWeek = date.getDay();
console.log(dayOfWeek); // Output: The current day of the week

Example 5: Using Date.getDay() with Past and Future Dates

In this example, we use Date.getDay() to get the day of the week for past and future dates.

let pastDate = new Date('2000-01-01');
let futureDate = new Date('2050-05-20');
console.log(pastDate.getDay()); // Output: 6 (Saturday)
console.log(futureDate.getDay()); // Output: 5 (Friday)

Example 6: Mapping Day of the Week to Names

In this example, we map the numeric day of the week to the corresponding day name.

let date = new Date('2024-07-15');
let daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let dayName = daysOfWeek[date.getDay()];
console.log(dayName); // Output: "Monday"

4. Conclusion

In this chapter, we explored the Date.getDay() method in TypeScript, which is used to return the day of the week for the specified date according to local time. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Date.getDay() effectively can help in various scenarios where extracting the day of the week from a date object is required.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare