gpt4 book ai didi

JavaScript:如何将 UTC 日期/时间转换为山区时间?

转载 作者:行者123 更新时间:2023-11-29 20:48:58 24 4
gpt4 key购买 nike

我一直在尝试编写一个脚本,它将获取丹佛的当前时间并将其输出到一个 URL 中。

我已经能够做到这一点:http://jsfiddle.net/Chibears85/h41wu8vz/4/

JS

$(function() {
var today = new Date();
var ss = today.getUTCSeconds();
var nn = today.getUTCMinutes() - 3; //3 minute delay
var hh = today.getUTCHours() - 6; //Offset UTC by 6 hours (Mountain Time)
var dd = today.getUTCDate();
var mm = today.getUTCMonth() + 1; //January is 0!
var yyyy = today.getUTCFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
if (hh < 10) {
hh = '0' + hh
}

var today = mm + '/' + dd + '/' + yyyy + '%20' + hh + ':' + nn + ':' + ss ;
$('img.r').each(function() {
var url = $(this).attr('src');
if (url.indexOf("?") >= 0) {
$(this).attr("src", url + today);
} else {
$(this).attr("src", url + "?feature_date=" + today);
}
});
});

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="screen.js"></script>
<img class="r" src="https://mywebsite.com&DateTime=" width="400">

它将日期插入到 URL 中,但是从山区时间下午 6 点到凌晨 12 点,时间会中断(01:00:00 10/20/2018 变为 -5:00:00 10/20/2018 而不是 19:00:00 10/19/2018) 和 3 分钟的延迟偏移使其从 :00-:02 每小时中断一次(1:01 变为 1:-02 而不是 00:59)。

我想知道如何修复 UTC 偏移量,这样它就不会减去负数并适本地偏移日期/月份/年份。

最佳答案

根据您的浏览器支持需求,您可以使用 toLocaleString但请注意 locales and options may not be supported in Edge and are not supported in Android webview .

new Date().toLocaleString('en-US', {timeZone: 'America/Denver'})

要按照您的函数得出结论并手动将 UTC 时间转换为山地时间(山地标准时间或山地夏令时,具体取决于一年中的时间),您必须扩展您的函数以处理夏令时。例如(这就是为什么像 Moment.js 这样的库如此受欢迎,并且可能值得研究以满足您的需求):

const twoDigit = (d) => (d < 10 ? '0' : '') + d;
const formatDate = (date, time) => {
date = date.map((x) => twoDigit(x)).join('/');
time = time.map((x) => twoDigit(x)).join(':');
return `${date} ${time}`;
};

const getOffset = (month, date, day, hour) => {
// assume MST offset
let offset = 7;

// adjust to MDT offset as needed
if ((month > 2 && month < 10) || (month === 2 && date > 14)) {
offset = 6;
} else if (month === 2 && date > 7 && date < 15) {
if ((day && date - day > 7) || (day === 0 && hour - offset >= 2)) {
offset = 6;
}
} else if (month === 10 && date < 8) {
if ((day && date - day < 0) || (day === 0 && hour - offset < 1)) {
offset = 6;
}
}

return offset;
};

const getMountainTime = () => {
const dt = new Date(); // current datetime
let year = dt.getUTCFullYear(); // utc year
let month = dt.getUTCMonth(); // utc month (jan is 0)
let date = dt.getUTCDate(); // utc date
let hour = dt.getUTCHours(); // utc hours (midnight is 0)
let minute = dt.getUTCMinutes(); // utc minutes
let second = dt.getUTCSeconds(); // utc seconds
let day = dt.getUTCDay(); // utc weekday (sunday is 0)
let offset = getOffset(month, date, day, hour);
if (hour - offset < 0) {
hour = 24 + hour - offset;
day = day ? day - 1 : 6;
if (date === 1) {
if (!month) {
year -= 1;
month = 11;
} else {
month -= 1;
}

date = new Date(year, month + 1, 0).getDate();
} else {
date -= 1;
}
} else {
hour -= offset;
}

month += 1;
return formatDate([month, date, year], [hour, minute, second]);
};

const denver = getMountainTime();
console.log(denver);

关于JavaScript:如何将 UTC 日期/时间转换为山区时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52897912/

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