Home > other >  HTML JavaScript to add Date format not working correctly
HTML JavaScript to add Date format not working correctly

Time:12-10

I put the my html code to Australia/Sydney Date format, but its not working correctly, it always shown currant date, like as Friday, December 9, 2022 dose any one know the solution?

Australia date now Saturday, December 10, 2022

Thank you

here is my code

const datesausDiv = document.getElementById('date-div-aus');

function myDateFunction() {
  const now = new Date();
  const timeZones = ['Australia/Sydney'];
  const options = {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  };
  const nowStr = now.toLocaleString('en-US', options);
  datesausDiv.innerHTML = nowStr;
}

setInterval(myDateFunction, 1000);
<div id="date-div-aus"> </div>

CodePudding user response:

The wording of the question is pretty confusing, but it sounds like you're trying to display the current date in Australia to users who are not necessarily in Australia.

You defined a "timezone" array but didn't do anything with it; it needs to be fed in as one of the options:

const datesausDiv = document.getElementById('date-div-aus');

function myDateFunction() {
  const now = new Date();
  const options = {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    timeZone: "Australia/Sydney"
  };
  const nowStr = now.toLocaleString('en-US', options);
  datesausDiv.innerHTML = nowStr;
}

setInterval(myDateFunction, 1000);
<div id="date-div-aus"> </div>

(Note: to the likely resigned chagrin of the residents of Perth, the above pretends that Australia has only one timezone.)

CodePudding user response:

Based on this wiki, it says that "Australians typically write the date with the day leading, as in the United Kingdom and New Zealand".

This means that dates have these possible formats:

  • 4 December 2022
  • 2022-12-04 or 04/12/2022

Therefore, the only fix you need is to use en-GB.

Please let me know if this is what you were searching for, else provide an example of the format you wish the date to show up. Have a nice day.

const datesausDiv = document.getElementById('date-div-aus');

function myDateFunction() {
  const now = new Date();
  const options = {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  };
  const nowStr = now.toLocaleString('en-GB', options);
  datesausDiv.innerHTML = nowStr;
}

setInterval(myDateFunction, 1000);
<div id="date-div-aus"> </div>

CodePudding user response:

The following demonstrates how you can present the same (current) time in different formats and for different global locations:

const now = new Date();

console.log("Sydney:",now.toLocaleString('en-GB', {timeZone:"Australia/Sydney"}));
console.log("New York:",now.toLocaleString('en-US', {timeZone:"America/New_York"}));
console.log("here:",now.toLocaleString());
 
<div id="date-div-aus"> </div>

CodePudding user response:

Split the string on each forward slash to get the day, month and year. Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.

  • Related