Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
remove timezone in my random date generator in javascript

This is my code. I make it random but the timezone is always in there and i dont know how to disappear the timezone, can someone help me with this I'am beginner in this thanks

var startDate = new Date("1990-01-01"); //YYYY-MM-DD
var endDate = new Date("2022-01-01"); //YYYY-MM-DD

function formatDate(date) {
  const year = date.getFullYear();
  /* getMonth returns dates from 0, so add one */
  const month = date.getMonth() + 1;
  const day = date.getDate();
  
  return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
}

var getDateArray = function(start, end) {

  return new Date(
    start.getTime() + Math.random() * (end.getTime(0) - start.getTime(0))
  );

}
var dateArr = getDateArray(startDate, endDate);


console.log(dateArr);

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

If you call the formatDate function you have in your code, I believe that will get rid of the timezone information.

var startDate = new Date("1990-01-01"); //YYYY-MM-DD
var endDate = new Date("2022-01-01"); //YYYY-MM-DD

function formatDate(date) {
  const year = date.getFullYear();
  /* getMonth returns dates from 0, so add one */
  const month = date.getMonth() + 1;
  const day = date.getDate();
  
  return `${year}-${month < 10 ? '0' : ''}${ month }-${ day < 10 ? '0' : '' }${day}`
}

var getDateArray = function(start, end) {

  return formatDate(new Date(
    start.getTime() + Math.random() * (end.getTime(0) - start.getTime(0))
  ));

}
var dateArr = getDateArray(startDate, endDate);


console.log(dateArr);

All I did here was add a call to your formatDate function within the getDateArray function so that now the return of the getDateArray will no longer contain timezone information.

about 4 years ago · Santiago Gelvez Relatório

0

Create these 2 functions to change the date format.

function DateFormat(startDate)
    {
        const strYear = date("Y",strtotime(strDate));
        const strMonth= date("n",strtotime(strDate));
        const strDay= date("j",strtotime(strDate));
        return "${strYear}$-${strMonth}-${strDay}";
    }

function DateFormat(endDate)
    {
        const strYear = date("Y",strtotime(strDate));
        const strMonth= date("n",strtotime(strDate));
        const strDay= date("j",strtotime(strDate));
        return "${strYear}$-${strMonth}-${strDay}";
    }
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda