首先我要说的是我对javascript没有深入的了解,但我知道如何以足够的方式为页面编写小脚本。我的客户需要我为网站做以下事情:
到目前为止,我已经能够创建从本地计算机获取时间的逻辑。我以为我可以使用datejs,但它不会计算一天中的小时数。
这是我目前的代码:
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var suffix = "AM";
if (hours >= 12)
{
suffix = "PM";
hours = hours - 12;
}
var suffix = "AM";
if (hours >= 12)
{
suffix = "PM";
hours = hours - 12;
}
if (hours == 0)
{
hours = 12;
}
if (minutes < 10)
minutes = "0" + minutes;
document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");
答案 0 :(得分:3)
这个怎么样:
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (minutes < 10)
minutes = "0" + minutes
if (suffix == "PM" && hours >= 6)
{
document.write("You're too late for next day shipping!");
}
else
{
var hoursLeft = 5 - hours;
var minsLeft = 60 - minutes;
document.write("<b> You've got " + hoursLeft + " hours and " + minsLeft + " minutes left to qualify for next day shipping! </b>")
}
答案 1 :(得分:2)
如果这个网站让我对其他人的答案发表评论,我会把这个问题归功于Giovanni,但由于我还不能评论其他人的工作,所以这里需要改变。
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var suffix = "AM";
if (minutes < 10)
minutes = "0" + minutes
if (hours >= 18)
{
document.write("You're too late for next day shipping!");
}
else
{
var hoursLeft = 17 - hours;
var minsLeft = 60 - minutes;
if(minsLeft==60){
minsLeft=0;
hoursLeft++;
}
document.write("<b> You've got " + hoursLeft + " and " + minsLeft + " minutes left to qualify for next day shipping! </b>")
}
原因是那些在凌晨5点订购的人可能会认为他们必须在下一个小时内提交他们的运送到第二天,而实际上他们有接下来的13个小时。
编辑:看到了您的时区问题,here是您可能感兴趣的帖子。
编辑2:发布了错误的链接。现在应该是正确的,但这可能是一个过时的答案。
答案 2 :(得分:0)
我昨天也解决了类似问题,所以这很容易。这是javascript代码:
function start_onload(last_hour){
var timeout_message = document.getElementById('timeout_message');
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var expire_time = 0; // in seconds
if (hours<last_hour) {
expire_time += (last_hour-hours-1)*3600;
expire_time += (59-minutes)*60;
expire_time += (59-seconds);
}
else {
timeout_message.innerHTML = 'It\'s after '+last_hour+' o\'clock!';
return;
}
var expire_time = currentTime.getTime() + 1000*expire_time;
//console.log(expire_time, hours, minutes, seconds, expire_time);
function countdown_session_timeout() {
var current_time = new Date().getTime();
var remaining = Math.floor((expire_time - current_time)/1000);
if (remaining>0) {
hours = Math.floor(remaining/3600);
minutes = Math.floor((remaining - hours*3600)/60);
seconds = remaining%60;
timeout_message.innerHTML = 'Countdown will stop in '+ hours + ' hours ' + minutes + ' min. ' + seconds + ' sec.';
setTimeout(countdown_session_timeout, 1000);
} else {
timeout_message.innerHTML = 'Time is up!';
}
}
countdown_session_timeout();
}
完整脚本@ pastebin.com就在这里。
答案 3 :(得分:0)
这是一个简单的倒计时器,从功能运行30秒开始,到0结束。到达0后自动重置计数器。再过30秒,这个过程继续循环
window.onload = function() { startCountDown(30, 1000, myFunction); } function startCountDown(i, p, f) { var pause = p; var fn = f; var countDownObj = document.getElementById("countDown"); countDownObj.count = function(i) { //write out count countDownObj.innerHTML = i; if (i == 0) { //execute function //fn(); startCountDown(30, 1000, myFunction); //stop return; } setTimeout(function() { // repeat countDownObj.count(i - 1); }, pause ); } //set it going countDownObj.count(i); } function myFunction(){};</script>