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

254
Visualizações
Node.js moment timezone conversion not working as expected

So this is what the below code should do. I am essentially making objects that expire at the midnight mark of the different timezones. The way I think I should do this is to first take user location and find the corresponding timezone. From there, take the current date in that timezone and make a time-formatted string (currentTimestamp) so it expires at the end of the day at 11:59:59 PM. Then convert this time to UTC (because that's how the server operates) and then obviously I end up removing the object from my database when the time is passed. So for example, I'm trying to get this to work in the Shanghai timezone, but I'm getting a weird and very wrong value for UTC moment. Here's what it looks like:

let timezone = tzlookup(latitude, longitude);
let currentDate = new Date()
let localizedDateAndTime = moment.tz(currentDate, timezone).format('YYYY-MM-DD hh:mm:ss')
let localizedDate = moment.tz(currentDate, timezone).format('YYYY-MM-DD')
console.log("Timezone: " + timezone)
console.log("Local Time: " + localizedDateAndTime)
console.log("UTC Time: " + moment.tz(currentDate, 'UTC').format())
let date = ("0" + currentDate.getDate()).slice(-2);
let month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
let year = currentDate.getFullYear();
let currentTimestamp = localizedDate + " " + 23 + ":" + 59 + ":" + 59
console.log("Localized timestamp: " + currentTimestamp)
var localizedMoment = moment.tz(currentTimestamp, 'YYYY-MM-DD hh:mm:ss', timezone);
var utcMoment = localizedMoment.utc()
console.log("UTC Moment (when event expires in UTC): " + utcMoment.format('YYYY-MM-DD hh:mm:ss'))
let timestamp = utcMoment.format('YYYY-MM-DD hh:mm:ss')

https://i.stack.imgur.com/deRbS.png

So I guess it shouldn't be 03:59:59 for the time, but rather something like 15:59:59, right? Not sure why it converts to 03:59:59...

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

0

This is really an extended comment.

Your code is way too complicated. Anytime you think you need to do date operations by creating and parsing strings, you should rethink what you're doing. In this case, you can just create a Date, set the timezone, set it to end of day, then generate your timestamp as UTC, e.g.

let loc = 'Asia/Shanghai';
let currentDate = new Date()
let localDate = moment.tz(currentDate, loc);
localDate.endOf('day');
console.log(localDate.utc().format('YYYY-MM-DD HH:mm:ss'));

// One statement:
console.log(
  moment.tz(new Date(), loc)
    .endOf('day')
    .utc()
    .format('YYYY-MM-DD HH:mm:ss')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data.min.js"></script>

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