Javascript date get day month

Javascript date get day month

Notice that we added 1 to the output of the Date.getMonth method.

Copied!
const date = new Date(); const month = date.getMonth() + 1; console.log(month); // 👉️ 1

This is because months are zero-based in JavaScript, so 0 is January and 11 is December.

  • Date.getFullYear — returns a four-digit number representing the year of the date.
  • Date.getDate — returns an integer between 1 and 31 representing the day of the month of the date.

If the month and day have a value of less than 10 , the getMonth and getDate methods will return single-digit values.

You can pad the results with a leading zero to always format the month and day as 2 digits.

Copied!
// 👇️ args are YYYY, MM, DD const date = new Date(2026, 0, 5); function padTo2Digits(num) return num.toString().padStart(2, '0'); > const year = date.getFullYear(); const month = padTo2Digits(date.getMonth() + 1); const day = padTo2Digits(date.getDate()); const withSlashes = [year, month, day].join('/'); console.log(withSlashes); // 👉️ 2026/01/05 const withHyphens = [year, month, day].join('-'); console.log(withHyphens); // 👉️ 2026-01-05

We used the String.padStart method to pad the month and day if their values are less than 10 .

The method takes the total length of the string and the pad string as parameters.

Copied!
function padTo2Digits(num) return num.toString().padStart(2, '0'); > console.log(padTo2Digits(4)); // 👉️ '04' console.log(padTo2Digits(8)); // 👉️ '08' console.log(padTo2Digits(10)); // 👉️ '10'

Now the month and day are guaranteed to have 2 digits.

The previous example gets the year, month and day using the visitor’s local time. If you need to get the UTC equivalent, use the methods from the next code snippet.

The difference between UTC and local time is that UTC (Coordinated Universal Time) is not a time zone, but a time standard and it’s the basis for time zones worldwide. No country uses UTC as a local time.

Copied!
const date = new Date(); const year = date.getUTCFullYear(); const month = date.getUTCMonth() + 1; const day = date.getUTCDate(); const withSlashes = [year, month, day].join('/'); console.log(withSlashes); // 👉️ 2023/1/4 const withHyphens = [year, month, day].join('-'); console.log(withHyphens); // 👉️ 2023-1-4

We used the UTC equivalent of the getFullYear , getMonth and getDate methods.

These methods return the year, month and day according to universal time.

Just like the getMonth method, the getUTCMonth method also returns an integer from 0 (January) to 11 (December).

If you have to get the year, month and date from a date object often, define a reusable function.

Copied!
function padTo2Digits(num) return num.toString().padStart(2, '0'); > function getYearMonthDay(date = new Date()) const year = date.getFullYear(); const month = padTo2Digits(date.getMonth() + 1); const day = padTo2Digits(date.getDate()); return year, month, day>; > const year, month, day> = getYearMonthDay(new Date(2026, 0, 5)); console.log(year); // 👉️ 2026 console.log(month); // 👉️ 01 console.log(day); // 👉️ 05

The getYearMonthDay function takes a Date object as a parameter and returns the year, month and day of the given date.

If the function is called without a Date object, it returns the current year, month and day.

# Get the Current Year, Month and Day in JavaScript

To get the current year, month and day:

  1. Use the new Date() constructor to create a Date object.
  2. Use the getFullYear() method to get the current year.
  3. Use the getMonth() method to get the current month.
  4. Use the getDate() method to get the current day.
Copied!
const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; const currentDay = date.getDate(); const together = [currentYear, currentMonth, currentDay].join('/'); console.log(together); // 👉️ 2023/1/4

We used the Date() to get a Date object on which we can call various methods.

The new Date() constructor returns an object that stores the current date and time.

Copied!
console.log(new Date()); // 👉️ 2023-01-04T06:12:14.208Z

We called the following methods on the Date object:

  • Date.getFullYear — returns a four-digit number representing the year of the date.
  • Date.getMonth — returns an integer between 0 (January) and 11 (December) and represents the month of the date. Yes, unfortunately, the getMonth method is off by 1 .
  • Date.getDate — returns an integer between 1 and 31 representing the day of the month of the date.

The getFullYear() and getDate() methods work as expected, however, the getMonth() method is zero-based.

Copied!
const date = new Date(); console.log(date); // 👉️ 2023-01-04T06:14:13.781Z const currentYear = date.getFullYear(); console.log(currentYear); // 👉️ 2023 const currentMonth = date.getMonth() + 1; console.log(currentMonth); // 👉️ 1 const currentDay = date.getDate(); console.log(currentDay); // 👉️ 4

This is why we added 1 to the result of calling the getMonth() method.

You can use the Intl.DateTimeFormat object to get the name of the current month in different languages, e.g. English and German.

Copied!
// 👇️ Get Names of Month instead of Digits const nameOfMonthUS = new Intl.DateTimeFormat('en-US', month: 'long'>).format( new Date(), ); console.log(nameOfMonthUS); // 👉️ January const nameOfMonthDE = new Intl.DateTimeFormat('de-DE', month: 'long'>).format( new Date(), ); console.log(nameOfMonthDE); // 👉️ Januar console.log(new Date()); // 👉️ 2023-01-04T06:15:28.157Z

The getFullYear , getMonth and getDate methods allow us to get the year, month and day of any Date object.

If you have to get the current year, month and day often, define a reusable function.

Copied!
function currentYearMonthDay() const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; const currentDay = date.getDate(); return currentYear, currentMonth, currentDay>; > const currentYear, currentMonth, currentDay> = currentYearMonthDay(); console.log(currentYear); // 👉️ 2023 console.log(currentMonth); // 👉️ 1 console.log(currentDay); // 👉️ 4

The function returns an object containing the current year, month and day.

You can use the String.join() method if you need to format the values in any way.

Copied!
const date = new Date(); function padTo2Digits(num) return num.toString().padStart(2, '0'); > const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; const currentDay = date.getDate(); // 👇️ 2023/01/04 console.log( [currentYear, padTo2Digits(currentMonth), padTo2Digits(currentDay)].join('/'), ); // 👇️ 2023-01-04 console.log( [currentYear, padTo2Digits(currentMonth), padTo2Digits(currentDay)].join('-'), );

The Array.join() method concatenates all of the elements in an array using a separator.

The only argument the Array.join() method takes is a separator — the string used to separate the elements of the array.

All you have to do is pass the specific date to the Date() constructor to initialize the date.

Copied!
const date = new Date('September 24, 2025 15:24:23'); const yearOfDate = date.getFullYear(); // 👉️ 2025 const monthOfDate = date.getMonth() + 1; // 9 const dayOfMonth = date.getDate(); // 24 const together = [yearOfDate, monthOfDate, dayOfMonth].join('/'); console.log(together); // 👉️ 2025/9/24

Always keep in mind that the getMonth method is zero-based, like the index of an array or string.

To get an intuitive result, add 1 to the return value of the method.

# Get the Day, Month, Year from a Timestamp in JavaScript

To get the day, month and year values from a timestamp:

  1. Pass the timestamp to the Date() constructor to create a Date object.
  2. Use the getFullYear() , getMonth() and getDate() methods to get the year, month and day values.
Copied!
const timestamp = 1643200384959; const date = new Date(timestamp); console.log(date); // 👉️ Wed Jan 26 2022 const year = date.getFullYear(); console.log(year); // 👉️ 2022 const month = date.getMonth(); console.log(month); // 👉️ 0 (January = 0, February = 1, etc) const monthName = date.toLocaleString('default', month: 'long', >); console.log(monthName); // 👉️ "January" const day = date.getDate(); console.log(day); // 👉️ 26

You can pass a timestamp (in milliseconds) to the Date() constructor to create a Date object.

We used the following 3 date-related methods:

  • Date.getFullYear method — returns a four-digit number representing the year that corresponds to a date.
  • Date.getMonth — returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately, the getMonth method is off by 1 .
  • Date.getDate — returns an integer between 1 and 31 representing the day of the month for a specific date.

Note that the `getMonth` method returns a zero-based value, where January is 0 , February is 1 , March is 2 , etc.

# Getting the Day, Month and Year from a timestamp according to UTC

The getFullYear , getMonth and getDate methods return the values according to the visitor’s local time, if you need to get the year, month and day according to universal time, use the getUTC* methods instead.

Copied!
const timestamp = 1643200384959; const date = new Date(timestamp); console.log(date); // 👉️ Wed Jan 26 2022 const year = date.getUTCFullYear(); console.log(year); // 👉️ 2022 const month = date.getUTCMonth(); console.log(month); // 👉️ 0 (January = 0, February = 1, etc) const monthName = date.toLocaleString('default', month: 'long', >); console.log(monthName); // 👉️ January const day = date.getUTCDate(); console.log(day); // 👉️ 26

The getUTC* methods return the values according to universal time.

There could be a difference between the output from the get* methods and the getUTC* methods if the visitor’s timezone has an offset from UTC (Universal Coordinated Time).

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Date.prototype.getDate()

The getDate() method of Date instances returns the day of the month for this date according to local time.

Try it

Syntax

Return value

An integer, between 1 and 31, representing the day of the month for the given date according to local time. Returns NaN if the date is invalid.

Examples

Using getDate()

The day variable has value 25 , based on the value of the Date object xmas95 .

const xmas95 = new Date("1995-12-25T23:15:30"); const day = xmas95.getDate(); console.log(day); // 25 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jun 1, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Читайте также:  Php mysql через pdo
Оцените статью