Javascript Data Type How to - Get Time in seconds from specific date until now in seconds








Question

We would like to know how to get Time in seconds from specific date until now in seconds.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.0.2.js'></script>
<script type='text/javascript'>
<!-- ww w  . j av a  2s  .  c  o m-->
function get_uptime() {
    var t1 = new Date(2013, 00, 00, 0, 0, 0, 0)
    var t2 = new Date()
    var dif = t1.getTime() - t2.getTime()
    seconds = dif / 1000;
    Seconds_Between_Dates = Math.abs(seconds);
    document.writeln(Seconds_Between_Dates);
    document.writeln('<br/>');
    setTimeout(get_uptime, 1000);
}
get_uptime();

</script>
</head>
<body>

</body>
</html>

The code above is rendered as follows: