当打印时钟的时间时,类似的代码工作并针对所选时区进行调整,但这不适用于打印日期。任何想法为什么?为什么日期不适合UTC设置的时间?
它只显示UTC默认时间。
<script>
function cetDT(){
var now = new Date();
var today = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var day = today.getDate();
var month = today.getMonth();
var year = today.getFullYear();
var anHour = 1000 * 60 * 60;
today = new Date(today.getTime() - anHour * -2);
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
if (hours >= 12){
meridiem = "";
}
else {
meridiem = "";
}
if (minutes<10){
minutes = "0" + minutes;
}
else {
minutes = minutes;
}
if (seconds<10){
seconds = "0" + seconds;
}
else {
seconds = seconds;
}
document.getElementById("cetDT").innerHTML = (day + '/' + (parseFloat (month) + 1) + '/' + year);
}
cetDT();
</script>
您正在使用'now.getUTCDate()'和'now.getUTCHours()',它将获取UTC中的当前日期和时间。 'getDate','getHours'等将获取与时区相关的时间。 –
但是我也在稍后用“today = new Date(today.getTime() - anHour * -2);”来修改它。或者至少我在尝试....任何建议? – ebkod
这段代码有很多错误的东西。你究竟在努力完成什么? –