Why is it returning undefined for the month. JS date
Solution 1:
The getMonth()
method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year). So now in January it return 0. And arr[month-1]
would be arr[-1]
which is undefined.
Solution 2:
Month is 0-index, so remove the -1
in month-1
will work (if you move January to the beginning of the array).
Since now in January, it will use -1
which is incorrect.
The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
Correct way to do that:
var date = new Date();
var month = date.getMonth();
var arr = [
'січня',
'лютого',
'березня',
'квітня',
'травня',
'червня',
'липня',
'серпня',
'вересня',
'жовтня',
'листопада',
'грудня',
];
console.log(arr[month])
$('.rf_title').text('Данi оновлено ' + date.getDate() + ' ' + arr[month] + ' ' + date.getFullYear() +' о ' + (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' + '' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>