• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

355
Views
How can I obtain the date based on geographical coordinates? (JavaScript)

I am making a weather application, and I have a simple P-tag which is populated with the date using JavaScript:

HTML:

<p id="date"></p>

JavaScript:

function getDate() {
    var shortDate = document.getElementById("date");
    var today = new Date().toLocaleDateString(undefined, {
        weekday: "long",
        month: "long",
        day: "numeric"
    });
    shortDate.innerHTML = today;
}

My issue is that it always presents the date where I am (which is understandable). Though when I physically change the latitude and longitude to locations with a different date in my JavaScript (such as Australia), it still uses the date where I am, and not that of the coordinates I have amended for the purpose of testing this.

I'm still learning JavaScript, but how can I overcome this to ensure my program uses the date of the different location? Or even using the timezone of the location? When I amended my JavaScript to Australia coordinate, it didn't show the date in Australia!

Thank you ๐Ÿ™‚

about 3 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

I answered my own question! I had to use the offset provided by the API and multiply by 1000 ๐Ÿ™‚

let today = new Date(Date.now() + offset * 1000).toLocaleDateString(undefined, {
    weekday: "long",
    month: "long",
    day: "numeric"
    });

shortDate.innerHTML = today;
about 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
ยฉ 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error