gpt4 book ai didi

javascript 获取之间的时间

转载 作者:行者123 更新时间:2023-12-01 03:20:25 26 4
gpt4 key购买 nike

我有一个使用 google api 返回时间的 ajax。我想要的是如果时间在上午 9:30 到晚上 7:30 之间输出一些数据。然而我的条件不行。您对此有什么建议或想法吗?预先感谢您。

这个条件不起作用。

if ((currentHours >= 9) && currentMinutes >= 30 && (currentHours <= 19) && currentMinutes <= 30) {  }

这是我的ajax。

var loc = '35.652832, 139.839478' // Tokyo expressed as lat,lng tuple
var targetDate = new Date() // Current date/time of user computer
var timestamp = targetDate.getTime() / 1000 + targetDate.getTimezoneOffset() * 60 // Current UTC date/time expressed as seconds since midnight, January 1, 1970 UTC
var apikey = 'myKey'
var apicall = 'https://maps.googleapis.com/maps/api/timezone/json?location=' + loc + '&timestamp=' + timestamp + '&key=' + apikey

$.ajax({
async: false,
type: "POST",
url: apicall,
success: function (response) {
var offsets = response.dstOffset * 1000 + response.rawOffset * 1000 // get DST and time zone offsets in milliseconds
var localdate = new Date(timestamp * 1000 + offsets) // Date object containing current time of Tokyo (timestamp + dstOffset + rawOffset)
var currentHours = localdate.getHours();
var currentMinutes = localdate.getMinutes();
var currentSeconds = localdate.getSeconds();
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
currentTimeString = currentHours + ":" + currentMinutes;
console.log('currentimestring ' + currentTimeString);
// console.log(currentMinutes);
if ((currentHours >= 9) && currentMinutes >= 30 && (currentHours <= 21) && currentMinutes <= 30) {
_isSupportHoursResult = true;
console.log(_isSupportHoursResult);
if (_isSupportHoursResult) {
jpsupp = true;
console.log(jpsupp);
} else {
// window.location = "/jpprechatform/OffSupportRedirect.aspx?accessID=" + inAccessID;
jpsupp = false;
}
} else {
console.log('closing time');
}
console.log(localdate.toLocaleTimeString()) // Display current Tokyo date and time
},
error: function (xmlHttpRequest, error) {
_isSupportHoursResult = false;
},
complete: function (response) {
}
});

最佳答案

根据您的情况,您已使用(currentMinutes >= 30 && currentMinutes <= 30)因为它变得矛盾。这似乎是问题所在。你可以使用:

if((currentHours > 9 && currentHours < 19) || (currentHours === 9 && currentMinutes >= 30) || (currentHours === 19 && currentMinutes <= 30)){}

关于javascript 获取之间的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45253880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com