[英]Change variable by firing JavaScript date event
Hi I am working on a project where client wants to update a variable at 12:00 PM嗨,我正在做一个项目,客户想在 12:00 PM 更新一个变量
Technologies:技术:
here is a basic guide of how this can be done...这是如何完成此操作的基本指南...
// Set the next delivery date
var NextDelivery = null;
function CalcTimeOut(){
var cutOffTime = new Date();
var currentTime = new Date();
var difference = null;
if(cutOffTime.getHours() < 12) {
// If we are before 12 we can deliver today
cutOffTime.setHours(12,0,0,0);
NextDelivery = new Date();
} else {
// Set new cutoff time
cutOffTime.setHours(36,0,0,0);
// If we are after 12 we can only deliver tomorrow.
var currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 1);
currentDate.setHours(0,0,0,0);
NextDelivery = currentDate;
}
var difference = (cutOffTime.getTime() - currentTime.getTime());
// Recalculate if we need to...
setTimeout(function(){
CalcTimeOut();
}, difference);
}
// Call from where ever you need to.
CalcTimeOut();
I looked at the code ofHuhammad Ahmod and It really helped me to fix the issue.我查看了Huhammad Ahmod的代码,它确实帮助我解决了这个问题。 by the way here is the fix of the problem I derived.
顺便说一下,这里是我导出的问题的修复。 index.html
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="app.js"></script>
<title>Document</title>
</head>
<body>
<input type="date" id="date">
</body>
</html>
and in app.js I did the following things:在app.js中我做了以下事情:
$(document).ready(function (){
// Set the next delivery date
var NextDelivery = null;
$('#date').change(function (){
// // CalcTimeOut();
// // today();
// difference();
Updated();
});
function Updated(){
var current=new Date();
var selected=document.getElementById('date').value;
var selectedDate=new Date(selected);
var uDate;
if (selectedDate.getDate()==current.getDate() && selectedDate.getMonth()==current.getMonth()){
if (current.getHours()<12){
uDate=current.getDate();
}
else{
uDate=current.getDate()+1;
}
}
else{
uDate=selectedDate.getDate();
}
var uMonth;
if (selectedDate.getMonth()+1<10){
var next=selectedDate.getMonth()+1;
uMonth='0'+next;
}
else {
uMonth = selectedDate.getMonth() + 1;
}
if (uDate<10){
uDate='0'+uDate;
}
var orderDate = selectedDate.getFullYear()+'-'+uMonth+'-'+uDate;
console.log(orderDate);
document.getElementById('date').value=orderDate;
}
});
Less then 10 was used to fix the format issue of YYYY-MM-DD error given by input type="Date" Less then 10 用于修复 input type="Date" 给出的 YYYY-MM-DD 错误的格式问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.