计算开始日期时间和结束日期时间之间的时间。自动增量像计时器 [英] Calculate time between start datetime and end datetime & auto increment like timer
问题描述
我试图显示开始日期时间和结束日期时间之间的自动增量时间。它只适用于开始日期时间和结束日期时间的同一天。如果开始和结束日期时间不同,则无法正常工作。对于这些我尝试下面的代码。
我尝试过:
JavaScript代码
< script type = text / javascript>
function toSeconds(time_str){
var parts = time_str.split(< span class =code-string>' :');
返回部分[ 0 ] * 3600 +
部件[ 1 ] * 60 +
+
部分[ 2 ];
}
function StartTimer(startTimeP,endTimeP){
var difference = Math .abs(toSeconds(startTimeP) - toSeconds(endTimeP));
// console.log(差异);
var result = [
Math .floor(difference / 3600 ),
数学 .floor((差异% 3600 )/ 60 ),
差异% 60
];
var x = setInterval( function (){
result [ 2 ] =结果[ 2 ] + 1 ;
if (结果[ 2 ]> = 60 ){
result [ 2 ] = 0 ;
结果[ 1 ] =结果[ 1 ] + 1 ;
}
if (结果[ 1 ]> = 60 ){
结果[ 1 ] = 0 ;
结果[ 0 ] =结果[ 0 ] + 1
}
结果[ 0 ] =结果[ 0 ] === 24 ? 0 :结果[ 0 ]
var temp = result.map( function (v){
var tempResult = v< 10 ?' 0' + v:v;
return tempResult;
})。join(' :');
// console.log(temp);
$ ( .working_time)。text(temp);
}, 1000 );
}
function Say(){
// document.getElementById('ancClick')。click();
$( #AncClick)。click();
}
< / script>
Default.aspx文件中的标签
< asp:标签 ID = lblTrackWorkedHours runat = server CssClass = working_time 工具提示 = 工作时间 > < / asp:Label >
Default.aspx。 cs
ScriptManager.RegisterStartupScript(Page,GetType(), myscript, StartTimer('11 :34:25 PM','15:55:38 PM');, true );
这里 11:34:25 PM 27-02-2019 11:34:25 PM 和 15:55:38 PM 28-02-2019 15:55:38 PM 。所以,如果我计算结果应 16 Hours + 。但它显示 7+小时。这是错误的计算。
因此建议我们在增加装置后正确显示它应该是像计时器一样增量自动。
( .working_time)。text(temp);
}, 1000 );
}
function Say(){
// document.getElementById('AncClick')。click();
( #AncClick)。click();
}
< / script>
Default.aspx文件中的标签
< asp:标签 ID = lblTrackWorkedHours runat = server CssClass = working_time 工具提示 = 工作时间 > < / asp:Label >
Default.aspx。 cs
ScriptManager.RegisterStartupScript(Page,GetType(), myscript, StartTimer('11 :34:25 PM','15:55:38 PM');, true );
这里 11:34:25 PM 27-02-2019 11:34:25 PM 和 15:55:38 PM 28-02-2019 15:55:38 PM 。所以,如果我计算结果应 16 Hours + 。但它显示 7+小时。这是错误的计算。
所以建议我们正确地显示增量装置,因为它应该像计时器一样自动增量。
使用日期和时间的任何语言的最佳方式是使用它们各自的DateTime格式,而不是将其解析为字符串。
当你正在使用它时,实际的变量才刚刚开始是数字,并且更容易使用,因为它是简单的减法。
我所做的是将你的变量定义为Dates然后减去。因为这是以毫秒为单位,除以1000得到秒。一旦经过几秒钟,您就可以轻松转换为分钟和小时var date1 = new 日期(' 2019-02-27T23: 34:25' 跨度>);
var date2 = new 日期(' 2019-02-28T15:55:38');
var ElapsedSeconds =(date2 - date1)/ 1000 ;
// var ElapsedHours = ElapsedSeconds / 3600;
I am tried to show the auto increment time between start datetime and end datetime. And it's working fine for only for same day where start datetime and end datetime. If start and end datetime is different it's not working correctly. For these I tried below code.
What I have tried:
JavaScript Code
<script type="text/javascript">
function toSeconds(time_str) {
var parts = time_str.split(':');
return parts[0] * 3600 +
parts[1] * 60 +
+
parts[2];
}
function StartTimer(startTimeP, endTimeP) {
var difference = Math.abs(toSeconds(startTimeP) - toSeconds(endTimeP));
//console.log(difference);
var result = [
Math.floor(difference / 3600),
Math.floor((difference % 3600) / 60),
difference % 60
];
var x = setInterval(function () {
result[2] = result[2] + 1;
if (result[2] >= 60) {
result[2] = 0;
result[1] = result[1] + 1;
}
if (result[1] >= 60) {
result[1] = 0;
result[0] = result[0] + 1
}
result[0] = result[0] === 24 ? 0 : result[0]
var temp = result.map(function (v) {
var tempResult = v < 10 ? '0' + v : v;
return tempResult;
}).join(':');
//console.log(temp);
$(".worked_time").text(temp);
}, 1000);
}
function Say() {
//document.getElementById('AncClick').click();
$("#AncClick").click();
}
</script>
And Label in Default.aspx file
<asp:Label ID="lblTrackWorkedHours" runat="server" CssClass="worked_time" ToolTip="Worked Hours"></asp:Label>
Default.aspx.cs
ScriptManager.RegisterStartupScript(Page, GetType(), "myscript", "StartTimer('11:34:25 PM','15:55:38 PM');", true);
Here 11:34:25 PM is 27-02-2019 11:34:25 PM and 15:55:38 PM is 28-02-2019 15:55:38 PM . So if I calculate result should be 16 Hours +. but it's showing 7+ Hours. It's calculating wrong.
So suggest us to display correctly with increment means after resulting it should be increment auto like timer.
(".worked_time").text(temp); }, 1000); } function Say() { //document.getElementById('AncClick').click();
("#AncClick").click(); } </script>
And Label in Default.aspx file
<asp:Label ID="lblTrackWorkedHours" runat="server" CssClass="worked_time" ToolTip="Worked Hours"></asp:Label>
Default.aspx.cs
ScriptManager.RegisterStartupScript(Page, GetType(), "myscript", "StartTimer('11:34:25 PM','15:55:38 PM');", true);
Here 11:34:25 PM is 27-02-2019 11:34:25 PM and 15:55:38 PM is 28-02-2019 15:55:38 PM . So if I calculate result should be 16 Hours +. but it's showing 7+ Hours. It's calculating wrong.
So suggest us to display correctly with increment means after resulting it should be increment auto like timer.
The best way in any language to work with dates and time is to work with them in their respective DateTime format and not parsing it as a string.
While you are working with this the actual variables are just going to be numbers, and are much easier to work with as it is simple subtraction.
What I have done is defined your variables as Dates and then subtracted. As this is in milliseconds I divided by 1000 to get seconds. Once you have elapsed seconds you can easily convert to minutes and hoursvar date1 = new Date('2019-02-27T23:34:25'); var date2 = new Date('2019-02-28T15:55:38'); var ElapsedSeconds = (date2 - date1) / 1000; // var ElapsedHours = ElapsedSeconds / 3600;
这篇关于计算开始日期时间和结束日期时间之间的时间。自动增量像计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!