环境: Win 10 Pro、Chrome 85、luxon 1.25.0
我试图最终实现的是:在ASP.Net/c#应用程序中,不断向用户显示会话超时之前的时间量。因为服务器和客户端可能在不同的时区,所以我需要它们的UTC偏移量。客户端的偏移量很容易获得。要找到服务器的偏移量(参考下面的代码片段):服务器代码将页面的上次加载时间放入lblLastLoaded。对象dto接收解析的日期部分,包括dto.offset,我认为它应该以分钟表示。随后对luxon.DateTime.fromObject(dto)的调用失败:未定义pst。当我没有设置dto.offset时(通过在下面的代码中注释掉4行),pst成功地获得了服务器时间,但没有偏移量;它似乎使用了我本地系统的偏移量-7小时。
<script>
function r4onload() {
// Get server time:
var st = document.getElementById('lblLastLoaded').innerHTML;
// Date Time Offset
// st has form YYYY/MM/dd HH:mm:ss -HH:mm
// 1 2
// 012345678901234567890123456
// For Example 2020/09/29 10:31:56 -07:00
let dto = {};
dto.year = Number(st.substring(0, 4));
dto.month = Number(st.substring(5, 7));
dto.day = Number(st.substring(8, 10));
dto.hour = Number(st.substring(11, 13));
dto.minute = Number(st.substring(14, 16));
var offsetHour = Number(st.substring(20, 23)); // works if these 4 lines are commented out
var offsetMinutes = Number(st.substring(24, 26)); // works if these 4 lines are commented out
dto.offset = offsetHour * 60 + Math.sign(offsetHour) * offsetMinutes; // works if these 4 lines are commented out
// dto.offset is correctly calculated to -420 minutes // works if these 4 lines are commented out
var pst = luxon.DateTime.fromObject(dto);
// pst is undefined at this point -- why??
// If I do not include anything about offset above (comment out the 4 lines
// containing offsetHours, offsetMinutes, and dto.Offset, then pst comes out like this:
// 2020-09-29T10:31:00.00000-07:00
// i.e., luxon used the offset -07:00 of my local system, not the one contained in variable st.
document.getElementById('Parsed').innerHTML = pst;
r4startTime();
}
function r4startTime() {
var dt = luxon.DateTime.local();
var h = dt.hour;
var m = dt.minute;
var s = dt.second;
m = r4checkTime(m);
s = r4checkTime(s);
document.getElementById('CurrTime').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(r4startTime, 2000);
}
function r4checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>文件Site.Master中包含上述代码片段的完整Visual Studio2017项目DemoLuxon位于此处:https://1drv.ms/u/s!AvoFL8QrGVaTsQvbaB8-Zh7GdloV?e=ODjZW5
如果上面的方法很笨拙,我将非常感谢您提出一些更优雅的方法来确定客户端/服务器偏移量的差异。
发布于 2020-10-18 04:07:22
我设计了一个解决方法,如下所示,但原始问题尚未得到回答:
<script>
function r4onload() {
var st = document.getElementById('lblLastLoaded').innerHTML;
// Date Time Offset
// st has form YYYY/MM/dd HH:mm:ss -HH:mm
// 1 2
// 012345678901234567890123456
// For Example 2020/09/29 10:31:56 -07:00
// Transform to ISO format:
st = st.substring(0, 4) + '-' + st.substring(5, 7) + '-' + st.substring(8, 10)
+ 'T' + st.substring(11, 19) + ".000" + st.substring(20);
document.getElementById('Xformed').innerHTML = st;
var pst = luxon.DateTime.fromISO(st, { setZone: true });
document.getElementById('Parsed').innerHTML = pst;
r4startTime();
}
function r4startTime() {
var dt = luxon.DateTime.local();
var h = dt.hour;
var m = dt.minute;
var s = dt.second;
m = r4checkTime(m);
s = r4checkTime(s);
document.getElementById('CurrTime').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(r4startTime, 2000);
}
function r4checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>例如,先将日期-时间转换为ISO格式,然后使用luxon.DateTime.fromISO。变量pst获取正确的日期-时间值,包括偏移量。具有偏移量的luxon.DateTime.fromObject的问题仍未解决。
https://stackoverflow.com/questions/64127669
复制相似问题