Метод getMonth()
повертає місяць у вказаній даті згідно з місцевим часом, як значення на основі нуля (де нуль позначає перший місяць року).
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Синтаксис
dateObj.getMonth()
Повертає
Ціле число між 0 та 11, що відображає місяць у наданій даті згідно з місцевим часом. 0 відповідає січню, 1 лютому, і так далі.
Приклади
Використання getMonth()
Друга інструкція, наведена нижче, присвоює значення 11 змінній month
, на основі значення об'єкта Date
на ім'я Xmas95
.
var Xmas95 = new Date('December 25, 1995 23:15:30');
var month = Xmas95.getMonth();
console.log(month); // 11
Заувага: За необхідності можна отримати повну назву місяця (наприклад, "січень"
) за допомогою методу Intl.DateTimeFormat()
з параметром options
. З цим методом інтернаціоналізація стає простішою:
var options = { month: 'long'};
console.log(new Intl.DateTimeFormat('en-US', options).format(Xmas95));
// December
console.log(new Intl.DateTimeFormat('uk', options).format(Xmas95));
// грудень
Специфікації
Специфікація |
---|
ECMAScript (ECMA-262) The definition of 'Date.prototype.getMonth' in that specification. |
Сумісність з веб-переглядачами
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.