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 ๐
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;