var date = new Date();
var month = date.getMonth();
var arr = [
'лютого',
'березня',
'квітня',
'травня',
'червня',
'липня',
'серпня',
'вересня',
'жовтня',
'листопада',
'грудня',
'січня',
];
$('.rf_title').text('Данi оновлено ' + date.getDate() + ' ' + arr[month-1] + ' ' + date.getFullYear() +' о ' + (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' + '' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()));
I’m confused as to why this is showing undefined. It worked fine up until New Years. As soon as 2022 hit it’s done for.enter image description here
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>
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.