Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
JS Format 201708 into M yy format

I have a string in this format 201708 where the first four numbers are the year and the last two the month's. The result being a date of August 2017.

My first idea was to just make it to an date but won't work for me

var formattedDate = new Date("201708")

Thanks for any help

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

Separate the date with a hyphen.

let b = "201708"
let b_with_hyphen = b.substring(0, 4) + "-" + b.slice(4)
// '2017-08'

let formattedDate = new Date(b_with_hyphen)
console.log(formattedDate.toUTCString())

about 4 years ago · Santiago Gelvez Denunciar

0

use substring() to split the string into year and month, then use those when calling Date().

let input = '201708';
let year = parseInt(input.substring(0, 4));
let month = parseInt(input.substring(5));
let date = new Date(year, month-1);
console.log(date);

You have to subtract 1 from month because JS counts months starting from 0.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda