JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS Date Get Methods

JavaScript is a popular programming language that is used to create interactive web pages. One of the most important features of JavaScript is its ability to work with dates and times. The Date object in JavaScript provides a number of methods that allow you to get and manipulate dates and times. In this article, we will explore the various get methods available in the Date object.

Brief Explanation of JS Date Get Methods

The Date object in JavaScript provides a number of get methods that allow you to retrieve various components of a date and time. These methods include:

  • getFullYear(): Returns the year of the specified date according to local time.
  • getMonth(): Returns the month of the specified date according to local time. The value returned by this method is an integer between 0 and 11, where 0 represents January and 11 represents December.
  • getDate(): Returns the day of the month for the specified date according to local time.
  • getDay(): Returns the day of the week for the specified date according to local time. The value returned by this method is an integer between 0 and 6, where 0 represents Sunday and 6 represents Saturday.
  • getHours(): Returns the hour of the specified date according to local time.
  • getMinutes(): Returns the minutes of the specified date according to local time.
  • getSeconds(): Returns the seconds of the specified date according to local time.
  • getMilliseconds(): Returns the milliseconds of the specified date according to local time.
  • getTime(): Returns the numeric value corresponding to the time for the specified date according to universal time.
  • getTimezoneOffset(): Returns the difference in minutes between the local time zone and UTC.

Code Examples

Let's take a look at some code examples that demonstrate how to use these get methods:


const now = new Date();

const year = now.getFullYear();
const month = now.getMonth();
const date = now.getDate();
const day = now.getDay();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const milliseconds = now.getMilliseconds();
const time = now.getTime();
const timezoneOffset = now.getTimezoneOffset();

console.log(year); // Output: 2021
console.log(month); // Output: 8 (September)
console.log(date); // Output: 15
console.log(day); // Output: 3 (Wednesday)
console.log(hours); // Output: 14
console.log(minutes); // Output: 30
console.log(seconds); // Output: 45
console.log(milliseconds); // Output: 123
console.log(time); // Output: 1631716245123
console.log(timezoneOffset); // Output: -240 (Eastern Daylight Time)

In this example, we create a new Date object and use the various get methods to retrieve the year, month, date, day, hours, minutes, seconds, milliseconds, time, and timezone offset of the current date and time. We then log these values to the console.

Conclusion

The get methods available in the Date object in JavaScript allow you to retrieve various components of a date and time. These methods are useful for working with dates and times in your JavaScript applications.

References

Activity