Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果用户当前时区与“开放时间”(ES6)一致,则显示div_Javascript_Json_Datetime_Ecmascript 6_Timezone - Fatal编程技术网

Javascript 如果用户当前时区与“开放时间”(ES6)一致,则显示div

Javascript 如果用户当前时区与“开放时间”(ES6)一致,则显示div,javascript,json,datetime,ecmascript-6,timezone,Javascript,Json,Datetime,Ecmascript 6,Timezone,在我的fetchapi技术中;首先,我要确保“办公时间”的存在;它会:上面的console.log打印以下数据: if (typeof(hoursoperation) != 'undefined' && hoursoperation != null) { console.log(myJson.acf['office_hours']); } 如何获取用户的本地时间;然后通过迭代上述内容进行比较 我知道使用jQuery是多么容易;但我在处理这一问题

在我的fetchapi技术中;首先,我要确保“办公时间”的存在;它会:上面的console.log打印以下数据:

if (typeof(hoursoperation) != 'undefined' && hoursoperation != null)
    {
      console.log(myJson.acf['office_hours']);



    }
如何获取用户的本地时间;然后通过迭代上述内容进行比较

我知道使用jQuery是多么容易;但我在处理这一问题时遇到了困难,因为这是普通的ES6 javaScript代码

i、 e.以下是我如何使用jQuery实现这一目标的:但我仍然在寻找ES6方法


首先,获取与办公时间相关的时区中的当前时间。由于您的源数据使用的是HHMM格式,因此也可以使用该格式获取时间:

            var today, starttime, endtime;

            var optiondata = JSON.parse(data);

            var officehours = optiondata.acf.office_hours;

            today = new Date();

            var starthour = parseInt(parseInt(officehours[today.getDay()].starting_time)/100);
            var startmin = starthour*100 - parseInt(officehours[today.getDay()].starting_time);

            var endhour = parseInt(parseInt(officehours[today.getDay()].closing_time)/100);
            var endmin = endhour*100 - parseInt(officehours[today.getDay()].closing_time);

            starttime = new Date(today.getFullYear(),today.getMonth(),today.getDate(),starthour,startmin,0,0);

            endtime = new Date(today.getFullYear(),today.getMonth(),today.getDate(),endhour,endmin,0,0);

            // calculate offset for timezones (PST or PDT) to ensure that AlgaeCal is actually open (using Vancouver, B.C.)

            var tzurl = "https://maps.googleapis.com/maps/api/timezone/json?location=49.246670,-123.094729&timestamp=" + parseInt(today.getTime()/1000) + "&key=XXXXX";

            $.ajax({
                url: tzurl,
                dataType: "text",
                success:function(tzdata){
                    $("#hours-operation").hide();

                    var timezonedata = JSON.parse(tzdata);

                    // subtract offsets to get GMT of start and end times of office hours and convert to milliseconds:
                    var chkstarttime = starttime.getTime() - timezonedata.dstOffset*1000 - timezonedata.rawOffset*1000;
                    var chkendtime = endtime.getTime() - timezonedata.dstOffset*1000 - timezonedata.rawOffset*1000;

                    // get offset for current local time and convert to milliseconds
                    var tz = today.toString().split("GMT")[1].split(" (")[0];
                    var chktoday = today.getTime() - parseInt(tz)/100*60*60*1000;

                    // check if current time is truly during open office hours:
                    if(chktoday >= chkstarttime && chktoday <= chkendtime){
                        // show "speak to our bone health specialists message"
                        $("#hours-operation").show();
                    }
                }
            });

        }
    });
现在,您可以简单地将值与源数据进行比较。这是因为HHMM格式的时间字符串是按字典顺序排序的

// This gives the current local time in Vancouver, in HHMM format, such as 1703 or 0945
const now = new Date().toLocaleTimeString('en-CA', {
    timeZone: 'America/Vancouver', hour12: false, timeStyle: 'short'
    }).replace(':','');
在上面的注释中,正常的班次类型是在同一天开始和停止的班次,例如0900到1700。倒班制是跨越午夜的一种,例如1800到0200

请注意,这仅适用于完全实现ECMAScript国际化API的环境,包括时区支持


还请注意,它不考虑在夏时制回退过渡的不明确时间内开始或结束的班次。完全围绕DST转换的班次应该没问题。

新日期将为您提供browserFYI的本地时间-您在第一篇文章中泄露了API密钥。虽然已编辑,但编辑内容仍然可见。即使删除帖子也没用,因为它对那些拥有足够高代表性的人来说仍然是可见的。你应该尽快用谷歌撤销API密钥并获得一个新的。
// This gives the current local time in Vancouver, in HHMM format, such as 1703 or 0945
const now = new Date().toLocaleTimeString('en-CA', {
    timeZone: 'America/Vancouver', hour12: false, timeStyle: 'short'
    }).replace(':','');
const isActive = starting_time <= closing_time   // test the shift type (normal or inverted)
    ? (starting_time <= now && closing_time > now)   // normal comparison
    : (starting_time <= now || closing_time > now);  // inverted comparison