简体   繁体   English

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

[英]show div if users current time with timezone aligns with 'open hours' (ES6)

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



    }

Above within my Fetch API technique;以上在我的 Fetch API 技术中; I make sure ' office_hours ' exists, first;我首先确保存在“ office_hours ”; and it does: the console.log above prints the following data:确实如此:上面的console.log打印以下数据:

Array(7)
0:
closing_time: "1600"
day: "7"
starting_time: "0800"
__proto__: Object
1:
closing_time: "1600"
day: "1"
starting_time: "0600"
__proto__: Object
2:
closing_time: "1600"
day: "2"
starting_time: "0600"
__proto__: Object
3: {day: "3", starting_time: "0600", closing_time: "1600"}
4: {day: "4", starting_time: "0600", closing_time: "1600"}
5: {day: "5", starting_time: "0600", closing_time: "1600"}
6: {day: "6", starting_time: "0700", closing_time: "1700"}
length: 7
__proto__: Array(0)

How could I get the users local time;我怎样才能获得本地时间的用户; then compare through iterate through the above?然后通过上面的迭代进行比较?

I know how easy this is with jQuery;我知道使用 jQuery 是多么容易; but am getting stuck on approaching this was vanilla ES6 javaScript code.但我一直坚持接近这个是香草 ES6 javaScript 代码。


ie Here is how I was able to get there with jQuery: (But still am searching for a ES6 method)这是我如何能够使用 jQuery 到达那里:(但仍在寻找 ES6 方法)

            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();
                    }
                }
            });

        }
    });

First, get the current time in the time zone relevant to the office hours.首先,获取与办公时间相关的时区的当前时间。 Since your source data is using HHMM format, get the time in that format also:由于您的源数据使用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(':','');

Now you can simply compare values with your source data.现在您可以简单地将值与源数据进行比较。 This works because time strings in HHMM format are lexicographically sortable.这是有效的,因为HHMM格式的时间字符串是按字典顺序排序的。

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

In the comments above, a "normal" shift type is one where the shift starts and stops on the same day, such as 0900 to 1700 .在上面的评论中,“正常”班次类型是班次在同一天开始和停止的班次,例如09001700 An "inverted" shift type is one that spans over midnight, such as 1800 to 0200 . “倒转”班次类型是跨越午夜的班次类型,例如18000200

Note that this only works in environments where the ECMAScript Internationalization API is fully implemented, including time zone support.请注意,这只适用于 ECMAScript 国际化 API 完全实现的环境,包括时区支持。

Also note that it does not take into account shifts that start or end during the ambiguous hour of a daylight saving time fall-back transition.另请注意,它不考虑在夏令时回退过渡的模糊小时内开始或结束的班次。 (Shifts that surround a DST transition entirely should be fine.) (完全围绕 DST 过渡的转变应该没问题。)

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

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