Date time Method in JavaScript

8/20/2021

Date time Method in JavaScript

Go Back

JavaScript Date & Time Methods: A Comprehensive Guide

 Date time Method in JavaScript

Introduction

JavaScript provides a robust set of built-in Date methods to handle date and time operations efficiently. Whether you need to retrieve the current date, format time, or manipulate timestamps, JavaScript’s Date object makes it simple. In this guide, we will explore various Date methods along with examples.

Creating a Date Object in JavaScript

Before using Date methods, you need to create a Date object in JavaScript.

var d = new Date();
console.log(d); // Outputs current date and time

JavaScript Date Methods

Below is a list of essential Date methods in JavaScript:

1. getDate() - Get the Day of the Month

Returns the numeric day of the month (1-31).

var d = new Date();
console.log(d.getDate()); // Outputs current day of the month

2. getDay() - Get the Day of the Week

Returns the day of the week as a number (0 for Sunday, 6 for Saturday).

console.log(d.getDay()); // Outputs a number from 0 to 6

3. getFullYear() - Get the Year

Returns the four-digit year.

console.log(d.getFullYear()); // Outputs current year

4. getMonth() - Get the Month

Returns the month as a number (0 for January, 11 for December).

console.log(d.getMonth()); // Outputs a number from 0 to 11

5. getHours() - Get the Hour

Returns the hour (0-23) in a 24-hour format.

console.log(d.getHours()); // Outputs current hour

6. getMinutes() - Get the Minutes

Returns the minutes (0-59).

console.log(d.getMinutes()); // Outputs current minutes

7. getSeconds() - Get the Seconds

Returns the seconds (0-59).

console.log(d.getSeconds()); // Outputs current seconds

8. toString() - Convert Date Object to String

Returns a string representation of the Date object.

console.log(d.toString());
// Example output: Mon Mar 07 2025 12:30:59 GMT+0530 (India Standard Time)

9. toDateString() - Get Date as String

Returns the date portion in a readable format.

console.log(d.toDateString());
// Example output: Mon Mar 07 2025

10. toTimeString() - Get Time as String

Returns the time portion of a Date object.

console.log(d.toTimeString());
// Example output: 12:33:39 GMT+0530 (India Standard Time)

Conclusion

JavaScript's Date object offers powerful methods to handle and manipulate dates and times. Whether you're working on a calendar, timestamping an event, or logging user activities, mastering these Date methods will enhance your JavaScript programming skills.

Stay Updated!

For more JavaScript tutorials and updates, follow us on [Instagram] and [Facebook]. 🚀

Table of content