javascript 使用javascript计算2个时间戳之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16767301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:54:48  来源:igfitidea点击:

Calculate difference between 2 timestamps using javascript

javascripttimestamp

提问by swateek

I have to calculate the difference between 2 timestamps. Also can you please help me with conversion of a string into timestamp. Using plain javascript only. NO JQUERY.

我必须计算 2 个时间戳之间的差异。也请您帮我将字符串转换为时间戳。仅使用纯 javascript。没有 JQUERY。

Here's my function:

这是我的功能:

function clearInactiveSessions()
{
    alert("ok");
    <c:if test="${not empty pageScope.sessionView.sessionInfo}">
        var currentTime = new Date().getTime();
        alert("curr:"+currentTime);
        var difference=new Date();
        <c:forEach items="${pageScope.sessionView.sessionInfo}" var="inactiveSession">
            var lastAccessTime = ${inactiveSession.lastUpdate};
            difference.setTime(Maths.abs(currentTime.getTime()-lastAccessTime.getTime()));
            var timediff=diff.getTime();
            alert("timediff:"+timediff);
            var mins=Maths.floor(timediff/(1000*60*60*24*60));
            alert("mins:"+mins);
            if(mins<45)
                clearSession(${item.sessionID});
        </c:forEach>
    </c:if>
}

回答by Anuj

i am posting my own example try implement this in your code

我正在发布我自己的示例尝试在您的代码中实现它

function timeDifference(date1,date2) {
    var difference = date1.getTime() - date2.getTime();

    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24

    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60

    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60

    var secondsDifference = Math.floor(difference/1000);

    console.log('difference = ' + 
      daysDifference + ' day/s ' + 
      hoursDifference + ' hour/s ' + 
      minutesDifference + ' minute/s ' + 
      secondsDifference + ' second/s ');
}

回答by Combine

Based on the approved answer:

根据批准的答案:

function(timestamp1, timestamp2) {
    var difference = timestamp1 - timestamp2;
    var daysDifference = Math.floor(difference/1000/60/60/24);

    return daysDifference;
}

回答by RobG

If your string is Mon May 27 11:46:15 IST 2013, you can convert it to a date object by parsing the bits (assuming 3 letter English names for months, adjust as required):

如果您的字符串是 Mon May 27 11:46:15 IST 2013,您可以通过解析位将其转换为日期对象(假设月份为 3 个字母的英文名称,根据需要进行调整):

// Convert string like Mon May 27 11:46:15 IST 2013 to date object 
function stringToDate(s) {
  s = s.split(/[\s:]+/);
  var months = {'jan':0, 'feb':1, 'mar':2, 'apr':3, 'may':4, 'jun':5, 
                'jul':6, 'aug':7, 'sep':8, 'oct':9, 'nov':10, 'dec':11};
  return new Date(s[7], months[s[1].toLowerCase()], s[2], s[3], s[4], s[5]);
}

alert(stringToDate('Mon May 27 11:46:15 IST 2013'));

Note that if you are using date strings in the same timezone, you can ignore the timezone for the sake of difference calculations. If they are in different timezones (including differences in daylight saving time), then you must take account of those differences.

请注意,如果您在同一时区使用日期字符串,则可以为了差异计算而忽略时区。如果它们处于不同的时区(包括夏令时的差异),那么您必须考虑这些差异。