[英]How to get the difference between two date and times and show the time running down
[英]How to get the difference between two date and times and show the time running down in Javascript
我有兩個變量我第一個變量我從日期時間選擇器中獲取日期和時間值,在第二個變量中我選擇當前日期和時間的值。 現在我想要它們之間的區別並在文本中顯示,例如計時器耗盡。
<input type="date" id="dates">
<input type="time" id="times">
var time=new Date().toLocaleTimeString('en-GB');
var res = time.slice(0,-3);
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
var current=today+" "+res; // Here i am getting the value of current date and time
var datetime=dates.value+" " +times.value; // This value is from date time picker
var t=datetime-current;
alert(t); //here the output is always NaN
我想要兩個日期時間之間的差異並顯示像 timmer run down 的文本
根據假定值更正您的代碼。
var dates = {
value: '2019-09-06'
};
var times = {
value: '10:55:00'
};
var time=new Date().toLocaleTimeString('en-GB');
var res = time.slice(0,-3);
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
var current=today+" "+res; // Here i am getting the value of current date and time
var datetime=dates.value+" " +times.value; // This value is from date time picker
var t= ( new Date( datetime ) ).getTime() - ( new Date( current ) ).getTime();
alert(t);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.