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

156
Views
Setting date and timezone is not working in ReactJS

I have a ReactJs project where also i use jest for testing. To display the time according UK timezone, i created this function:

 const getDateTZ = dt => {
  const d = new Date(dt)
  const o = {
    hour: '2-digit',
    minute: '2-digit',
    timeZone: 'Europe/London'
  }
  return d.toLocaleTimeString('en-GB', o)
}
console.log(getDateTZ(new Date()))

This function work fine and running it i get the right time. To test this function with jest i add this:

test('It should return time', () => {
  const today = new Date()
  today.setHours(11)
  today.setMinutes(30)
  const r = getDateTZ(today)
  expect(r).toEqual('09:30')
})
Note: The difference between my location and UK is 2 hours, UK is with 2 hours behind. The test passed. So running the test alone without running all tests it passes.
ISSUE: To run all tests from entire project in my package json i added this task: "test": "TZ=UTC jest the test above fails and output this when i do npm run test:

Expected: "09:30"
Received: "11:30"

So when i run all the tests my test fails because i receive 11:30 instead of 09:30 even if the test works if i run it separately in the test file.
Question: Why i get this issue and how to get rid of it?

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