PASSWORD RESET

Your destination for complete Tech news

How to get the current date in Javascript?

486 0
< 1 min read

To get the current date in JavaScript, you can use the Date object and its various methods. The Date object represents a specific point in time, and it provides several methods for accessing and manipulating the date and time.

Here are some examples of how to use the Date object to get the current date in JavaScript:

1. To get the current date and time as a string, you can use the toString method:

let currentDate = new Date().toString();

This will return the current date and time in the format “Fri Jul 16 2021 14:51:48 GMT-0700 (Pacific Daylight Time)”

2. To get the current year, month, and day as separate values, you can use the getFullYear, getMonth, and getDate methods:

let currentYear = new Date().getFullYear();
let currentMonth = new Date().getMonth();
let currentDay = new Date().getDate();

These methods return the current year, month, and day as integers. The getMonth method returns a value between 0 (for January) and 11 (for December).

3. To get the current hour, minute, and second as separate values, you can use the getHours, getMinutes, and getSeconds methods:

let currentHour = new Date().getHours();
let currentMinute = new Date().getMinutes();
let currentSecond = new Date().getSeconds();

These methods return the current hour, minute, and second as integers.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.