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

148
Visualizações
Convert String to Date then to timestamp to add to firestore

Okay so I've got two react native componenet DateTimePicker that I use to collect a date and time from the user using the following on change func

 const onChangeDate = (event, selectedDate) => {
    const currentDate = selectedDate || date;
    setDateShow(Platform.OS === 'ios');
    setDate(currentDate);

    let tempDate = new Date(currentDate);
    let date = tempDate.getFullYear() + '-' + tempDate.getMonth() + '-' + tempDate.getDate();

    console.log(date); //2022-4-18
    setFinalDate(date);
  }
  const onChangeTime = (event, selectedTime) => {
    const currentTime = selectedTime || time;
    setTimeShow(Platform.OS === 'ios');
    setTime(currentTime);

    let tempDate = new Date(currentTime);

    let time = tempDate.getHours() + ':' + tempDate.getMinutes() + ':' + tempDate.getSeconds() + tempDate.getMilliseconds();

    console.log(time); //0:10:00
    setFinalTime(time);
  }

After setting a state for finalTime and finalDate

const eventTimestamp = (`${finalDate} ${finalTime}`); 
console.log(eventTimestamp) // output is 2022-4-18 0:10:00

I cant seem to convert this into a date() object and then into a timestamp using toMillis() method becuse I want to add this into firestore as a timestamp. Converting it into a date using new Date() gives me Date { NaN } and using firestore method firebase.firestore.Timestamp.fromDate(works) gives me Object { "nanoseconds": NaN, "seconds": NaN, }

Any luck on where I'm going wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should parse the date string then convert it into a date() object. See sample code below:

// Initiate a new Date object.
const eventTimestamp = new Date(
  // Parse the string into a date
  Date.parse(
    `${finalDate} ${finalTime}`
    )
)

await setDoc(doc(db, "<collection>", "<document-id>"), {
    timestamp: eventTimestamp
});

For more information, you may checkout this documentation.

about 4 years ago · Juan Pablo Isaza 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