为什么 Safari/Opera 不能使用此 JavaScript 代码?

发布于 2024-08-29 23:31:52 字数 906 浏览 1 评论 0原文

我正在制作一个可以显示预订的日历。包含预订的高度是根据预订的长度按比例动态计算的。以下代码在 Firefox 中运行良好,但在 Safari 或 Opera 中运行不佳:

function calculateBookingHeight(from, to) {
    var today = new Date;
    var end = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
    var start = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);
    var from = new Date(from);
    var to = new Date(to);
    if (from > start && to < end) {
        var difference = (to - from) / 120000;
    } else if (from > start && to > end) {
        var difference = (end - from) / 120000;
    } else {
        var difference = 510
    }
    return difference;
}

总之,日历上每个小时的高度为 30px。第二个 if 语句处理预订在第二天结束的情况。

如果我用 return 510 替换整个代码块,Safari 会按预期运行,并将每个预订的高度设置为 510px,因此我认为一定是此函数中的某些内容导致了问题。

任何帮助将不胜感激。

谢谢罗宾

I am working on a calendar that will show bookings. The height of containing the booking is calculated dynamically in proportion to the length of the booking. The following code works perfectly well in Firefox but not in Safari or Opera:

function calculateBookingHeight(from, to) {
    var today = new Date;
    var end = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
    var start = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);
    var from = new Date(from);
    var to = new Date(to);
    if (from > start && to < end) {
        var difference = (to - from) / 120000;
    } else if (from > start && to > end) {
        var difference = (end - from) / 120000;
    } else {
        var difference = 510
    }
    return difference;
}

In summary, each hour on the calendar is a height of 30px. The second if statement deals with the end of the booking being the following day.

If I replace the entire code block with return 510, Safari behaves as expected and sets the height of each booking to 510px so I assume it must be something in this function that is causing the problem.

Any help would be appreciated.

Thanks

Robin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞慾 2024-09-05 23:31:52

你的问题之一应该是

var end = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
var start = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);

换句话说

var end = new Date(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
var start = new Date(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);

UTC 是一种 getter 方法,这就是你获取对象的原因。要使用这些参数构造日期,只需将它们传递给构造函数即可。

使用起来

var end = today ; 
end.setUTCHours(23) ;

会更直接。

One of your problems is that

var end = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
var start = new Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);

should be

var end = new Date(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),23);
var start = new Date(today.getUTCFullYear(),today.getUTCMonth(),today.getUTCDate(),6);

In other words, UTC is a getter method, which is why you were getting an object. To construct a date with those arguments, just pass them to the constructor.

Using

var end = today ; 
end.setUTCHours(23) ;

would be more straightforward.

最偏执的依靠 2024-09-05 23:31:52

http://www.w3schools.com/js/js_obj_date.asp

描述创建具有四个构造函数的 javascript 日期对象:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

正如 Luis 指出的那样,没有括号的调用不是有效的 js。

http://www.w3schools.com/js/js_obj_date.asp

Describes creation of a javascript date object with four constructors:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Your invocation without parens isn't valid js, as Luis points out.

雨巷深深 2024-09-05 23:31:52

想通了。未根据传递到函数的变量正确创建日期。我需要在创建日期之前单独解析输入。

Figured it out. The dates weren't being created properly from the variables passed into the function. I needed to separately parse the input before creating the dates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文