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

148
Views
.JS cambia la fecha AAAA/MM/DD a DD/MM/AAAA

Estoy creando un script de pasaporte que anota el nombre, apellido, fecha de nacimiento y altura de un usuario. Sin embargo, no puedo hacer que funcione más que con AAAA/MM/DD. js not es mi punto fuerte y me ha estado rompiendo la cabeza durante las últimas horas.

El código original relativo a todo lo centrado en la Fecha de Nacimiento es:

 'use strict' const DATE_FORMAT = '****/**/**' const DATE_PLACEHOLDER = 'YYYY/MM/DD' const birthdate = document.querySelector('#birthdate') function DoFormat(x, pattern, mask) { var strippedValue = x.replace(/[^0-9]/g, '') var chars = strippedValue.split('') var count = 0 var formatted = '' for (let i = 0; i < pattern.length; i++) { const c = pattern[i] if (chars[count]) { if (/\*/.test(c)) { formatted += chars[count] count++ } else { formatted += c } } else if (mask) { if (mask.split('')[i]) formatted += mask.split('')[i] } } return formatted; } function Format() { const val = DoFormat(birthdate.value, DATE_FORMAT) birthdate.value = DoFormat(birthdate.value, DATE_FORMAT, DATE_PLACEHOLDER) if (birthdate.createTextRange) { var range = birthdate.createTextRange() range.move('character', val.length) range.select() } else if (birthdate.selectionStart) { birthdate.focus() birthdate.setSelectionRange(val.length, val.length) } } Format() birthdate.addEventListener('input', Format)
 if (field.id === 'birthdate') { if (/^\d\d\d\d\/\d\d\/\d\d$/.test(field.value)) { let [yyyy, mm, dd] = field.value.split('/').map(p => parseInt(p, 10)) mm -= 1 const date = new Date(yyyy, mm, dd) if (date.getMonth() === mm && date.getDate() === dd && date.getFullYear() === yyyy) { if (date.getFullYear() >= LOWEST_YEAR && date.getFullYear() <= HIGHEST_YEAR) { return true } } } this.Error(field) return false }

cambiar DATE_FORMAT o la expresión no produce ningún resultado. ¿Alguien tiene una pista? También me encantaría usar DD-MM-YYYY en lugar de DD/MM/YYYY, pero DD/MM/YYYY es mejor que nada.

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

0

Una forma más fácil sería simplemente dividir la cadena por / e invertir la matriz resultante, luego volver a unirla por / :

 function reformat(str){ return str.split('/').reverse().join('/'); } console.log(reformat("10/11/1213"))

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!