简体   繁体   English

为什么Safari / Opera无法与此JavaScript代码一起使用?

[英]Why does Safari/Opera not work with this javascript code?

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: 以下代码在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;
}

In summary, each hour on the calendar is a height of 30px. 总而言之,日历上的每个小时都是30像素的高度。 The second if statement deals with the end of the booking being the following day. 第二个if语句涉及第二天的预订结束。

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. 如果我将整个代码块替换为return 510 ,则Safari会按预期方式运行,并将每个预订的高度设置为510px,因此我认为此功能一定是引起问题的原因。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

Robin 罗宾

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. 换句话说, UTC是一种吸气方法,这就是为什么要获取对象的原因。 To construct a date with those arguments, just pass them to the constructor. 要使用这些参数构造日期,只需将其传递给构造函数即可。

Using 使用

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

would be more straightforward. 会更直接。

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

Describes creation of a javascript date object with four constructors: 描述具有四个构造函数的javascript date对象的创建:

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. 正如Luis指出的那样,没有括号的调用不是有效的js。

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. 我需要在创建日期之前分别解析输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM