gpt4 book ai didi

javascript - 如何在 Javascript 中让 AM/PM 与时间一起正确工作

转载 作者:行者123 更新时间:2023-12-03 03:49:08 26 4
gpt4 key购买 nike

我有这个 Javascript 代码用于显示时间(根据时区)和日期,但问题在于 AM/PM。它不会随着时间的变化而自动更新;经过多次尝试,我无法自动更新,因此在这里寻求一些帮助。下面是代码:

$(document).ready(function(){
// Create two variable with the names of the months and days in an array
// Count 3 variable for months
var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

// Create a newDate() object
var newDate = new Date();
// Extract the current date from Date object
newDate.setDate(newDate.getUTCDate());
// Output the day, date, month and year
$('#Date').html(/*dayNames[newDate.getUTCDay()] + " " + */monthNames[newDate.getMonth()] + ' ' + newDate.getDate() + ', ' + newDate.getFullYear());

setInterval( function() {
// Create a newDate() object and extract the seconds of the current time on the visitor's
var seconds = new Date().getUTCSeconds();
// Add a leading zero to seconds value
$("#sec").html(( seconds < 10 ? "0" : "" ) + seconds);
},1000);

setInterval( function() {
// Create a newDate() object and extract the minutes of the current time on the visitor's
var minutes = new Date().getUTCMinutes();
// Add a leading zero to the minutes value
$("#min").html(( minutes < 10 ? "0" : "" ) + minutes);
},1000);

setInterval( function() {
// Create a newDate() object and extract the hours of the current time on the visitor's
var hours = new Date().getUTCHours();
var ampm = 'AM';
if(hours > 5)
{
hours -= 5; // Time Zone
ampm = 'AM'; // AM/PM According to Time Chosen
}
else if(hours == 5)
{
ampm = 'PM';
};

// Add a leading zero to the hours value
$("#hours").html(( hours < 10 ? "0" : "" ) + hours);

$("#ampm").html(ampm);
}, 1000);
});

<span id="hours"></span>:<span id="min"></span>&nbsp;<span id="ampm"></span>&nbsp;&nbsp;<span id="Date"></span>

最佳答案

我不太确定您要做什么,但是如果您尝试使用主机系统设置 UTC-0500 时区,然后以特定格式显示日期和时间,则会出现很多问题已经说到这里了。

一个简单的方法是获取主机日期,根据主机时区偏移调整时间并补偿默认的 5 小时。然后适本地格式化时间。时间以分钟为单位进行调整,因为这是 getTimezoneOffset 返回的内容,并且适合偏移量不是几小时的情况。

调整日期后,设置上午/下午非常简单:

var ampm = d.getHours() < 12? 'am' : 'pm';

根据需要经常调用该函数以显示当前时间。

// Return a date adjusted to UTC-0500
function getOffsetDate() {
var d = new Date();
// Set to UTC-0500: add timzone offset then subtract 300 mins
d.setMinutes(d.getMinutes() + d.getTimezoneOffset() - 300);
return d;
}

// Format time as hh:mm:ss ap
function formatTime(d) {
function z(n){return (n<10? '0' : '') + n}
var ap = d.getHours() < 12? 'am' : 'pm';
return z(d.getHours()) + ':' + z(d.getMinutes()) + ':' +
z(d.getSeconds()) + ' ' + ap;
}

function showTime() {
// Delay to just after next full second
var delay = 1020 - new Date().getMilliseconds();
// Show time
document.getElementById('time').textContent = formatTime(getOffsetDate());
// Call again
setTimeout(showTime, delay);
}

showTime();
<div>The time in timezone UTC-0500 is <span id="time"></span></div>

关于javascript - 如何在 Javascript 中让 AM/PM 与时间一起正确工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45241743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com