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

303
Views
¿Cómo acortar el bloque de mayúsculas y minúsculas convirtiendo un número en un nombre de mes?

¿Hay alguna manera de escribir esto en menos líneas, pero que sea fácil de leer?

 var month = ''; switch(mm) { case '1': month = 'January'; break; case '2': month = 'February'; break; case '3': month = 'March'; break; case '4': month = 'April'; break; case '5': month = 'May'; break; case '6': month = 'June'; break; case '7': month = 'July'; break; case '8': month = 'August'; break; case '9': month = 'September'; break; case '10': month = 'October'; break; case '11': month = 'November'; break; case '12': month = 'December'; break; }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Defina una matriz, luego obtenga por índice.

 var months = ['January', 'February', ...]; var month = months[mm - 1] || '';
over 4 years ago · Santiago Trujillo Report

0

¿Qué hay de no usar matriz en absoluto :)

 var objDate = new Date("10/11/2009"), locale = "en-us", month = objDate.toLocaleString(locale, { month: "long" }); console.log(month); // or if you want the shorter date: (also possible to use "narrow" for "O" console.log(objDate.toLocaleString(locale, { month: "short" }));

según esta respuesta Obtenga el nombre del mes de Fecha de David Storey

over 4 years ago · Santiago Trujillo Report

0

Prueba esto:

 var months = {'1': 'January', '2': 'February'}; //etc var month = months[mm];

Tenga en cuenta que mm puede ser un número entero o una cadena y seguirá funcionando.

Si desea que las claves inexistentes den como resultado una cadena vacía '' (en lugar de undefined ), agregue esta línea:

 month = (month == undefined) ? '' : month;

JSFiddle .

over 4 years ago · Santiago Trujillo 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!