I'm added New York City, United States time and date but I don't have any idea for the how to add current date and time following time zones. dose any one know how do that correctly using html and Java script
- New York City, United States
- London, United Kingdom
- Dubai, United Arab Emirates
- Sydney, Australia
- Mumbai, India
here is the New York City, United States time for I added code part
html
<p ><strong id="sec"></strong></p></div>
<div > Wednesday, November 23, 2022 </div>
javascript
$(document).ready(function() {
//EST
setInterval( function() {
var estTime = new Date();
var currentDateTimeCentralTimeZone = new Date(estTime.toLocaleString('en-US', { timeZone: 'America/Chicago' }));
var seconds = currentDateTimeCentralTimeZone.getSeconds();
var minutes = currentDateTimeCentralTimeZone.getMinutes();
var hours = currentDateTimeCentralTimeZone.getHours() 1;//new Date().getHours();
var am_pm = currentDateTimeCentralTimeZone.getHours() >= 12 ? "PM" : "AM";
if (hours < 10){
hours = "0" hours;
}
if (minutes < 10){
minutes = "0" minutes;
}
if (seconds < 10){
seconds = "0" seconds;
}
var mid='PM';
if(hours==0){ //At 00 hours we need to show 12 am
hours=12;
}
else if(hours>12)
{
hours=hours;
mid='AM';
}
var x3 = hours ':' minutes ':' seconds ' ' am_pm
// Add a leading zero to seconds value
$("#sec").html(x3);
},1000);
});
</script>
CodePudding user response:
I strongly suggest to use a library for this, as dealing with timezones and offsets is quite complex. My suggestion is to use [moment][1]
with its extension moment-timezone
.
You'll have to check the documentation to understand it, but a basic example for you situation would be something like this:
var moment = require('moment-timezone'); // make sure you install `moment` as well!
var date = moment(); // local datetime
var dateNewYork = moment('America/New_York'); // This won't work for every town, but New York is a standardized timezone indicator.
console.log(date.format('LLL'));
console.log(dateNewYork.format('LLL'));
CodePudding user response:
Add a loop
You can loop through the time zones, and run your existing program in each time zone
const timeZones = [
"America/Chicago",
"Europe/London",
// Add the other locations here, e.g. by looking through
// https://github.com/formatjs/date-time-format-timezone/tree/master/src/data/timezones
]
timeZones.forEach(timeZone => {
var estTime = new Date();
var currentDateTimeCentralTimeZone = new Date(estTime.toLocaleString('en-US', {
timeZone: timeZone
}));
var seconds = currentDateTimeCentralTimeZone.getSeconds();
var minutes = currentDateTimeCentralTimeZone.getMinutes();
var hours = currentDateTimeCentralTimeZone.getHours();
var am_pm = hours >= 12 ? "PM" : "AM";
if (hours < 10) {
hours = "0" hours;
}
if (minutes < 10) {
minutes = "0" minutes;
}
if (seconds < 10) {
seconds = "0" seconds;
}
var mid = 'PM';
if (hours == 0) { //At 00 hours we need to show 12 am
hours = 12;
} else if (hours > 12) {
hours = hours % 12;
mid = 'AM';
}
var x3 = hours ':' minutes ':' seconds ' ' am_pm
console.log(x3 " in " timeZone)
})