[英]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']);
}
以上在我的 Fetch API 技術中; 我首先確保存在“ office_hours
”; 確實如此:上面的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)
我怎樣才能獲得本地時間的用戶; 然后通過上面的迭代進行比較?
我知道使用 jQuery 是多么容易; 但我一直堅持接近這個是香草 ES6 javaScript 代碼。
即這是我如何能夠使用 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×tamp=" + 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(':','');
現在您可以簡單地將值與源數據進行比較。 這是有效的,因為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
在上面的評論中,“正常”班次類型是班次在同一天開始和停止的班次,例如0900
到1700
。 “倒轉”班次類型是跨越午夜的班次類型,例如1800
到0200
。
請注意,這只適用於 ECMAScript 國際化 API 完全實現的環境,包括時區支持。
另請注意,它不考慮在夏令時回退過渡的模糊小時內開始或結束的班次。 (完全圍繞 DST 過渡的轉變應該沒問題。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.