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

250
Views
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 answers
Answer question

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 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!