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

266
Views
How to obtain the timezone from open weather map and use it as a parameter using Vailla JS

I have a web page that display the weather based on the user input, also, I have a time HTML Element. The data will be initialized using JS. This is the code of the HTML Elements:

HTML:

<div class="content">
  <div class="date-time">
    <h1 class="time">
      00:00:00
    </h1>
    <h2 class="date">

    </h2>
  </div>
</div>

JavaScript:

setInterval(() => {
  // 12Hr Format For Hour
  function twilveFormat(hour) {
    if (hour >= 13) {
      return hour % 12;
    } else {
      return hour;
    }
  }

  function isAM_PM(hour) {
    if (hour >= 12) {
      return "AM";
    } else {
      return "PM";
    }
  }

  const days = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursay",
    "Friday",
    "Saturday"
  ];
  const months = [
    "Jan",
    "Feb",
    "March",
    "Apr",
    "May",
    "Jun",
    "July",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
  ];

  const time = new Date();

  const date = time.getDate();
  const month = time.getMonth();
  const day = time.getDay();
  const min = time.getMinutes();
  const sec = time.getSeconds();
  const hour = time.getHours();
  const hourIn12HR = twilveFormat(hour);
  const isAM = isAM_PM(hour);

  // Time
  document.querySelector(
    ".content .date-time .time"
  ).innerHTML = `${hourIn12HR}:${min}:${sec} <span id='AM_PM'>${isAM}</span>`;

  // Date

  document.querySelector(
    ".content .date-time .date"
  ).innerHTML = `${days[day]}, ${date} ${months[month]}`;
}, 1000);

I use Openweathermap API:

const res = await fetch(
  `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${this.apiKey}`
);
const data = await res.json();
this.display(data);

What I want is every time the user enters a city name using the below code of input, the timezone changes to the city that the user selected. The Openweathermap API have a timezone attribute but I don't know how to use it

HTML input:

<div action="" class="myform">
  <input type="" class="search" placeholder="Search">
  <button class="myBtn"><i class="fas fa-search"></i></button>
</div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To achieve your goal you first need to get the timezone-property and parse it to your need. By looking at the OpenWeatherMap API I can see that the response contains a "timezone"-property which gives you the shift in seconds from UTC time for that city. So what you can try: You can fetch the current time at your location using

var x = new Date();

Next you add the amount of seconds that the timezone-property holds. (of course you need to parse it to a number first) like so:

x.setSeconds(x.getSeconds + {timezoneParsed});

That will return you the city's current time as number of ticks. Next you just out put the current time in human readable format like so for instance:

x.toISOString();
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!