Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Why is it returning undefined for the month. JS date
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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!